繁体   English   中英

编译器不喜欢我的C ++类。 出现未声明的标识符错误等

[英]Compiler doesn't like my class C++. getting undeclared identifier error and more

我创建了一个名为“消息”的类。 我想将使用“消息”类创建的消息存储在名为“ MessageBox”的类的静态向量数组中。 编译器告诉我Message不存在,但是编辑器告诉我否则。 以下是带有代码的文件:

“ 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);

    };
}

“消息Box.cpp”

#include "Message Box.h"

void ATE::MessageBox::addMessage(Message msg)
{
     MsgBox.push_back(msg);
}

我的错误:

错误C2065'消息':未声明的标识符(文件:消息box.h,行:11)

错误C2923'std :: vector':'消息'不是参数'_Ty'的有效模板类型参数(文件:message box.h,行:11)

错误C2061语法错误:标识符“消息”(文件:message box.h,第12行)

非常感谢帮助(:

您的标头包括彼此。 由于#pragma once如果您首先在某些.cpp文件中包含Message.h ,则编译器将看到Message Box.h的内容在Message.h内部-恰好在#include "Message Box.h"行中。 结果,编译器将按以下顺序处理您的源代码:

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;

此时,使用了名称Message ,但是编译器尚未在Message.h达到其定义,并给您一个错误。

#pragma once删除#pragma once将无济于事。 您需要删除循环依赖项。 在这种情况下,从Message.h删除#include "Message Box.h" ,就可以了。

  1. 在“ Message.h”中,删除:

    #include "Message Box.h"

它添加了Message Box的前申报Message

  1. 在“ Message Box.cpp”中,在包括以下位置添加以下行:

    std::vector<ATE::Message> ATE::MessageBox::MsgBox;

必须在外部再次声明static成员。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM