0
votes

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 (:

2
"Message.h" #pragma once // do you forget pragma once? #include <string> // #include "Message Box.h" // why do you include message box in the message.h file? ... /// std::string ID2 = nullptr; // what do you mean?AnatolyS
yeah, i had the pragma once, but i forgot to copy it into this question. the #include "Message Box.h" in "Message.h" was from an old message system idea i had but then i started switching stuff around. std::string ID2 = nullptr is because it may never have a value. I took out the Message Box.h" in "Message.h" and now i'm getting link errorsNotHereAnymore
More in-depth discussion on what happened here: Resolve header include circular dependencies in C++user4581301
yeah, i hate refactoring code because i always have little bits left here and there. the 2nd part of the answer below got me out of the link errors. thank you all for your helpNotHereAnymore
You're compiling with Visual Studio, so be aware that windows.h has the nasty habit of #defining MessageBox. You might suddenly see an error on ATE::MessageBoxW, macro's do not respect scope.MSalters

2 Answers

1
votes

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.

0
votes
  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.