简体   繁体   中英

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". The compiler tells me that Message doesn't exist, but the editor is telling me otherwise. Here are the files with the code:

"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"

#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"

#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)

Error C2923 'std::vector': 'Message' is not a valid template type argument for parameter '_Ty' (file: message box.h, line: 11)

Error C2061 syntax error: identifier 'Message' (file: message box.h, line: 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. 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.

Removing the #pragma once won't help. You need to remove the circular dependency. In this case remove the #include "Message Box.h" from Message.h and you'll be fine.

  1. In "Message.h", remove the:

    #include "Message Box.h"

It add the Message Box declaration before the Message .

  1. In "Message Box.cpp" Add this line after the includes:

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

A static member must be declared again outside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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