【发布时间】:2016-06-23 05:13:57
【问题描述】:
我创建了一个名为“Message”的类。我想将使用“Message”类创建的消息存储在名为“MessageBox”的类中的静态向量数组中。编译器告诉我 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;
};
}
“消息框.h”
#pragma once
#include <string>
#include <vector>
#include "Message.h"
namespace ATE {
class MessageBox
{
public:
static std::vector<Message> MsgBox;
void addMessage(Message msg);
};
}
“消息框.cpp”
#include "Message Box.h"
void ATE::MessageBox::addMessage(Message msg)
{
MsgBox.push_back(msg);
}
我的错误:
错误 C2065 'Message':未声明的标识符(文件:message box.h,行:11)
错误 C2923 'std::vector': 'Message' 不是参数 '_Ty' 的有效模板类型参数(文件:message box.h,行:11)
错误 C2061 语法错误:标识符“消息”(文件:message box.h,行:12)
非常感谢您的帮助(:
【问题讨论】:
-
"Message.h" #pragma once // 你是否忘记了 pragma once? #include
// #include "Message Box.h" // 为什么要在 message.h 文件中包含消息框? ... /// std::string ID2 = nullptr; // 你是什么意思? -
是的,我有一次编译指示,但我忘了把它复制到这个问题中。 “Message.h”中的#include“Message Box.h”来自我的一个旧消息系统想法,但后来我开始切换东西。 std::string ID2 = nullptr 是因为它可能永远不会有值。我在“Message.h”中取出 Message Box.h”,现在出现链接错误
-
对这里发生的事情进行更深入的讨论:Resolve header include circular dependencies in C++
-
是的,我讨厌重构代码,因为我总是在这里和那里留下一点点。下面答案的第二部分让我摆脱了链接错误。谢谢大家的帮助
-
您正在使用 Visual Studio 进行编译,因此请注意
windows.h有 #definingMessageBox的讨厌习惯。您可能会突然在ATE::MessageBoxW上看到一个错误,宏不尊重范围。