此声明在 C++ 中没有存储类或类型说明符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22899466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
This declaration has no storage class or type specifier in C++
提问by Jain
I have multiple classes in my program .
我的程序中有多个类。
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 .
A)当我在另一个类中创建一个类的对象时,我没有收到任何错误,但是当我使用该对象调用一个函数时,我得到了上述错误。
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 .
B)此外,如果我创建另一个类的对象并在我的类的构造函数中使用它调用一个函数,那么我不会收到这样的错误。
C) Cout function does not work in the body of the class except when I put it any function
C) Cout 函数在类的主体中不起作用,除非我把它放在任何函数中
D) The main class is able to do all of these and I am not getting any error .
D) 主类能够完成所有这些并且我没有收到任何错误。
It would be great to hear back soon . Thank you in advance .
很快就会收到回复。先感谢您 。
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 ?
以下是代码:这是我的 cpp 中的两个类。除了在创建对象后使用对象外,我没有遇到任何问题。代码太大了,不贴了。一切都可以在 main 中完成,但不能在其他类中完成,为什么?
#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->
};
回答by M.M
This is a mistake:
这是个错误:
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.
类不会“运行”,它们提供了如何创建对象的蓝图。
The line Message m;
means that an Orderbook
will contain Message
called m
, if you later create an Orderbook
.
该行Message m;
表示 anOrderbook
将包含Message
被调用的m
,如果您稍后创建一个Orderbook
.
回答by msarafzadeh
Calling m.check(side), meaning you are running actual code, but you can't run code outside main() - you can only define variables. In C++, code can only appear inside function bodies or in variable initializes.
调用 m.check(side),意味着您正在运行实际代码,但您不能在 main() 之外运行代码 - 您只能定义变量。在 C++ 中,代码只能出现在函数体内或变量初始化中。
回答by Animesh Pathak
You can declare an object of a class in another Class,that's possible but you cant initialize that object. For that you need to do something like this :--> (inside main)
您可以在另一个类中声明一个类的对象,这是可能的,但您不能初始化该对象。为此,您需要执行以下操作:-->(在 main 中)
Orderbook o1;
o1.m.check(side)
but that would be unnecessary. Keeping things short :-
但这将是不必要的。保持简短:-
You can't call functions inside a Class
您不能在类中调用函数