Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

You must login to add post.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Passionable Logo Passionable Logo
Sign InSign Up

Passionable

Passionable Navigation

  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • New Questions
  • Trending Questions
  • Must read Questions
  • Hot Questions
Home/ Questions/Q 6846
In Process
Alek Richter
  • 0
Alek RichterEnlightened
Asked: December 22, 20212021-12-22T02:31:42+00:00 2021-12-22T02:31:42+00:00

“X does not name a type” error in C++

  • 0

I have two classes declared as below:

class User
{
public:
  MyMessageBox dataMsgBox;
};

class MyMessageBox
{
public:
  void sendMessage(Message *msg, User *recvr);
  Message receiveMessage();
  vector<Message> *dataMessageList;
};

When I try to compile it using gcc, it gives the following error:

MyMessageBox does not name a type

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
    • Report
  • Share
    Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Alek Richter Enlightened
    2021-12-22T02:32:27+00:00Added an answer on December 22, 2021 at 2:32 am

    When the compiler compiles the class User and gets to the MyMessageBox line, MyMessageBox has not yet been defined. The compiler has no idea MyMessageBox exists, so cannot understand the meaning of your class member.

    You need to make sure MyMessageBox is defined before you use it as a member. This is solved by reversing the definition order. However, you have a cyclic dependency: if you move MyMessageBox above User, then in the definition of MyMessageBox the name User won’t be defined!

    What you can do is forward declare User; that is, declare it but don’t define it. During compilation, a type that is declared but not defined is called an incomplete type. Consider the simpler example:

    struct foo; // foo is *declared* to be a struct, but that struct is not yet defined
    
    struct bar
    {
        // this is okay, it's just a pointer;
        // we can point to something without knowing how that something is defined
        foo* fp; 
    
        // likewise, we can form a reference to it
        void some_func(foo& fr);
    
        // but this would be an error, as before, because it requires a definition
        /* foo fooMember; */
    };
    
    struct foo // okay, now define foo!
    {
        int fooInt;
        double fooDouble;
    };
    
    void bar::some_func(foo& fr)
    {
        // now that foo is defined, we can read that reference:
        fr.fooInt = 111605;
        fr.foDouble = 123.456;
    }
    

    By forward declaring User, MyMessageBox can still form a pointer or reference to it:

    class User; // let the compiler know such a class will be defined
    
    class MyMessageBox
    {
    public:
        // this is ok, no definitions needed yet for User (or Message)
        void sendMessage(Message *msg, User *recvr); 
    
        Message receiveMessage();
        vector<Message>* dataMessageList;
    };
    
    class User
    {
    public:
        // also ok, since it's now defined
        MyMessageBox dataMsgBox;
    };
    

    You cannot do this the other way around: as mentioned, a class member needs to have a definition. (The reason is that the compiler needs to know how much memory User takes up, and to know that it needs to know the size of its members.) If you were to say:

    class MyMessageBox;
    
    class User
    {
    public:
        // size not available! it's an incomplete type
        MyMessageBox dataMsgBox;
    };
    

    It wouldn’t work, since it doesn’t know the size yet.


    On a side note, this function:

     void sendMessage(Message *msg, User *recvr);
    

    Probably shouldn’t take either of those by pointer. You can’t send a message without a message, nor can you send a message without a user to send it to. And both of those situations are expressible by passing null as an argument to either parameter (null is a perfectly valid pointer value!)

    Rather, use a reference (possibly const):

     void sendMessage(const Message& msg, User& recvr);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 4k
  • Best Answers 0
  • Users 200
  • Popular
  • Answers
  • Alek Richter

    When does a process get SIGABRT (signal 6)?

    • 2 Answers
  • Alek Richter

    How do I add sockets to an item?

    • 2 Answers
  • Alek Richter

    Dialog throwing "Unable to add window — token null is ...

    • 2 Answers
  • Alek Richter
    Alek Richter added an answer Pandas DataFrame columns are Pandas Series when you pull them… January 13, 2022 at 2:21 pm
  • Alek Richter
    Alek Richter added an answer The handshake failure could have occurred due to various reasons:… January 13, 2022 at 2:19 pm
  • Alek Richter
    Alek Richter added an answer Mac OS X doesn't have apt-get. There is a package… January 13, 2022 at 2:18 pm

Top Members

Alek Richter

Alek Richter

  • 4k Questions
  • 1k Points
Enlightened
fayemolloy0

fayemolloy0

  • 0 Questions
  • 20 Points
Begginer
NikolaZex

NikolaZex

  • 0 Questions
  • 20 Points
Begginer

Trending Tags

questin question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • New Questions
  • Trending Questions
  • Must read Questions
  • Hot Questions

© 2021 Passionable. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.