我有两个类声明如下:
class User
{
public:
MyMessageBox dataMsgBox;
};
class MyMessageBox
{
public:
void sendMessage(Message *msg, User *recvr);
Message receiveMessage();
vector<Message> *dataMessageList;
};
Run Code Online (Sandbox Code Playgroud)
当我尝试使用gcc编译它时,它会给出以下错误:
MyMessageBox没有命名类型
我不知道该搜索什么来找到解释,所以我在问.
我有这个代码报告错误:
struct Settings{
int width;
int height;
} settings;
settings.width = 800; // 'settings' does not name a type error
settings.height = 600; // 'settings' does not name a type error
int main(){
cout << settings.width << " " << settings.height << endl;
Run Code Online (Sandbox Code Playgroud)
但如果我将值赋值放在main中,它的工作原理如下:
struct Settings{
int width;
int height;
} settings;
main () {
settings.width = 800; // no error
settings.height = 600; // no error
Run Code Online (Sandbox Code Playgroud)
你能解释一下为什么吗?
编辑:
关于Ralph Tandetzky的回答,这是我的完整结构代码.你能告诉我如何像你的片段结构那样分配值吗?
struct Settings{
struct Dimensions{
int width;
int height; …Run Code Online (Sandbox Code Playgroud) 我得到一个问题,而返回类型是结构
Example.h
class Example {
private:
typedef struct connection_header {
string url;
string method;
};
static connection_header get_connection_header();
};
Example.cpp
connection_header Example::get_connection_header() {
return NULL;
}
Run Code Online (Sandbox Code Playgroud)
我正进入(状态 'connection_header' does not name a type
我可以知道为什么会出现这个错误