此声明在C ++中没有存储类或类型说明符 [英] This declaration has no storage class or type specifier in C++

查看:3091
本文介绍了此声明在C ++中没有存储类或类型说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有多个类。

I have multiple classes in my program .

A)当我在另一个类中创建一个类的对象时,我没有错误,但是当我使用该对象调用一个函数时,我得到了上面的错误。

A) When I create an object of a class in another class I am getting no error but when I use the object to call a function I get the above error .

B)另外,如果我创建另一个类的对象,并在我的类的构造函数中使用它调用一个函数,那么我不会得到这样的错误。

B)Also if I create an object of another class and call a function using that in the constructor of my class then I get no error like this .

C)Cout函数在类的主体中不起作用,除非我把它放入任何函数

C) Cout function does not work in the body of the class except when I put it any function

D)主类能够做所有这些,我没有得到任何错误。

D) The main class is able to do all of these and I am not getting any error .

很快就会收到回复。先谢谢。

It would be great to hear back soon . Thank you in advance .

以下是代码:这是我的cpp中的两个类。我面临没有问题,除了使用对象创建后。代码太大太贴了。一切都可以在主要完成,但不在其他类为什么?

Following is the code : These are two classes in my cpp. I am facing no problems except using object after creating it . the code is too huge too be posted . Everything can be done in main but not in other classes why ?

        #include <iostream>
        #include <fstream>
        #include <iomanip>
        #include <string>
        #include <cstdlib> 
        #include <vector>
        #include <map>
        using namespace std;
 class Message
  {

    public:
    void check(string side)
       {
    if(side!="B"&&side!="S")
          {
    cout<<"Side should be either Buy (B) or Sell (S)"<<endl;;
          }
       }
  };

    class Orderbook
  {
    public:
      string side;
      Orderbook()  //No Error if I define inside constructor
      Message m;       //No Error while declaring
      m.check(side);   //Error when I write m. or m->
  };


推荐答案

这是一个错误:

m.check(side);

代码必须放在一个函数中。
类定义只能包含声明和函数。

That code has to go inside a function. Your class definition can only contain declarations and functions.

类不会运行,它们提供了一个如何创建对象的蓝图。

Classes don't "run", they provide a blueprint for how to make an object.

Message m; 表示 Orderbook 将包含消息调用 m ,如果以后创建 Orderbook

The line Message m; means that an Orderbook will contain Message called m, if you later create an Orderbook.

这篇关于此声明在C ++中没有存储类或类型说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆