[英]Compiler doesn't like my class C++. getting undeclared identifier error and more
I created a class called "Message". 我创建了一个名为“消息”的类。 I want to store Messages that are created with the class "Message" in a static vector array in a class named "MessageBox".
我想将使用“消息”类创建的消息存储在名为“ MessageBox”的类的静态向量数组中。 The compiler tells me that Message doesn't exist, but the editor is telling me otherwise.
编译器告诉我Message不存在,但是编辑器告诉我否则。 Here are the files with the code:
以下是带有代码的文件:
"Message.h" “ Message.h”
#pragma once
#include <iostream>
#include <string>
#include "Message Box.h"
namespace ATE {
class Message
{
public:
Message(std::string act, std::string ID, std::string IDtwo) { action = act, ID1 = ID, ID2 = IDtwo; }
Message(std::string act, std::string ID) { action = act, ID1 = ID; }
std::string action;
std::string ID1;
std::string ID2 = nullptr;
};
}
"Message Box.h" “消息框.h”
#pragma once
#include <string>
#include <vector>
#include "Message.h"
namespace ATE {
class MessageBox
{
public:
static std::vector<Message> MsgBox;
void addMessage(Message msg);
};
}
"Message Box.cpp" “消息Box.cpp”
#include "Message Box.h"
void ATE::MessageBox::addMessage(Message msg)
{
MsgBox.push_back(msg);
}
My errors: 我的错误:
Error C2065 'Message': undeclared identifier (file: message box.h, line: 11) 错误C2065'消息':未声明的标识符(文件:消息box.h,行:11)
Error C2923 'std::vector': 'Message' is not a valid template type argument for parameter '_Ty' (file: message box.h, line: 11) 错误C2923'std :: vector':'消息'不是参数'_Ty'的有效模板类型参数(文件:message box.h,行:11)
Error C2061 syntax error: identifier 'Message' (file: message box.h, line: 12) 错误C2061语法错误:标识符“消息”(文件:message box.h,第12行)
Help is much appreciated (: 非常感谢帮助(:
Your headers include each other. 您的标头包括彼此。 Due to the
#pragma once
if you include Message.h
first in some .cpp file the compiler will see Message Box.h
's content "inside" Message.h
- exactly at the #include "Message Box.h"
line. 由于
#pragma once
如果您首先在某些.cpp文件中包含Message.h
,则编译器将看到Message Box.h
的内容在Message.h
内部-恰好在#include "Message Box.h"
行中。 As result the compiler will process your source code in the following order: 结果,编译器将按以下顺序处理您的源代码:
source.cpp:
//#include "Message.h"
message.h:
//#pragma once
//#include <iostream>
iostream:
//iostream's content
message.h(resumed):
//#include <string>
string:
//string's content
message.h(resumed):
#include "Message Box.h"
Message Box.h:
#pragma once
#include <string>
// string's content already included, won't include again
#include <vector>
// vector's content
#include "Message.h"
// message.h won't include again, due to the #pragma once
namespace ATE {
class MessageBox
{
public:
static std::vector<Message> MsgBox;
At this point the name Message
is used, but the compiler has not reached it's definition in Message.h
yet and gives you an error. 此时,使用了名称
Message
,但是编译器尚未在Message.h
达到其定义,并给您一个错误。
Removing the #pragma once
won't help. #pragma once
删除#pragma once
将无济于事。 You need to remove the circular dependency. 您需要删除循环依赖项。 In this case remove the
#include "Message Box.h"
from Message.h
and you'll be fine. 在这种情况下,从
Message.h
删除#include "Message Box.h"
,就可以了。
In "Message.h", remove the: 在“ Message.h”中,删除:
#include "Message Box.h"
It add the Message Box
declaration before the Message
. 它添加了
Message Box
的前申报Message
。
In "Message Box.cpp" Add this line after the includes: 在“ Message Box.cpp”中,在包括以下位置添加以下行:
std::vector<ATE::Message> ATE::MessageBox::MsgBox;
A static
member must be declared again outside. 必须在外部再次声明
static
成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.