Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

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 a question.

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.

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

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • 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
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7561221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:06:29+00:00 2026-05-30T13:06:29+00:00

This code is not compiling. What modification can I do to achieve the desired

  • 0

This code is not compiling.

What modification can I do to achieve the desired result?

ClassOne.h

#ifndef _CLASS_ONE_
#define _CLASS_ONE_

#include <string>
#include "ClassTwo.h"

class ClassTwo;

class ClassOne
{
private:
    string message;     
    friend ClassTwo;
    ClassTwo m_ClassTwo;

public:
    ClassOne();
    void Display();
};

#endif

ClassTwo.h

#ifndef _CLASS_TWO_
#define _CLASS_TWO_

#include <string>
#include "ClassOne.h"

class ClassOne;

class ClassTwo
{
private:
    string message;
    friend ClassOne;
    ClassOne m_ClassOne;

public:
    ClassTwo();
    void Display();
};

#endif

ClassOne.cpp

#include "ClassOne.h"
#include "ClassTwo.h"
#include <iostream>

ClassOne :: ClassOne()
{
    std::cout<<"ClassOne()...called\n";
    this->m_ClassTwo.message = "ClassOne - Message\n";
}

void ClassOne :: Display()
{
    std::cout<<this->m_ClassTwo.message;
}

ClassTwo.cpp

#include "ClassTwo.h"
#include "ClassOne.h"
#include <iostream>

ClassTwo :: ClassTwo()
{
    std::cout<<"ClassTwo()...called\n";
    this->m_ClassOne.message = "ClassTwo - Message\n";
}

void ClassTwo :: Display()
{
    std::cout<<this->m_ClassOne.message;
}

main.cpp

#include "ClassOne.h"
#include "ClassTwo.h"

int main()
{
    ClassOne one;
    one.Display();

    ClassTwo two;
    two.Display();
}

Error messages

1   error C2146: syntax error : missing ';' before identifier 'message'
2   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
3   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
4   error C2079: 'ClassTwo::m_ClassOne' uses undefined class 'ClassOne'
5   error C2146: syntax error : missing ';' before identifier 'message'
6   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
7   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
8   error C2039: 'message' : is not a member of 'ClassTwo'
9   error C2039: 'message' : is not a member of 'ClassTwo'
10  error C2146: syntax error : missing ';' before identifier 'message'
11  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
12  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
13  error C2079: 'ClassOne::m_ClassTwo' uses undefined class 'ClassTwo'
14  error C2146: syntax error : missing ';' before identifier 'message'
15  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
16  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
17  error C2039: 'message' : is not a member of 'ClassOne'
18  error C2039: 'message' : is not a member of 'ClassOne'
19  error C2146: syntax error : missing ';' before identifier 'message'
20  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
21  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
22  error C2079: 'ClassTwo::m_ClassOne' uses undefined class 'ClassOne'
23  error C2146: syntax error : missing ';' before identifier 'message'
24  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
25  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:06:31+00:00Added an answer on May 30, 2026 at 1:06 pm

    As said, you need to forward declare at least one of ClassOne or ClassTwo.

    You ClassOne.h could therefor look like:

    #ifndef _CLASS_ONE_
    #define _CLASS_ONE_
    
    #include <string>
    
    class ClassTwo;
    
    class ClassOne
    {
    private:
        string message;
        ClassTwo* m_ClassTwo;
    
    public:
        ClassOne();
        void Display();
    };
    
    #endif
    

    As you can see, we declare ClassTwo, but do not include it. We basically only tell the compiler that yes, we do have a ClassTwo, but we don’t really care what it contains right now.

    Also look at your ClassTwo member, this is now a pointer. The reason is that a member would require the compiler to know the size of the object, which we currently have no clue what is. Therefor you need either a pointer or a ref.

    Next, in your ClassOne.cpp, would will need to include ClassTwo.h, to get the functions and size of that class.

    A few things though:
    With a forward declaration you can not inherit from ClassTwo, use the methods of the forwarded class in the header file (How would the compiler know which methods exists?) Define functions or methods using the forwarded class, that is you have to pass by reference or pass a pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why this code does not compile (Cygwin)? #include <vector> template <class Ttile> class Tilemap
runat=server /> But this code is not working How can I add multiple controls
I want to specialize operator<< but this code is not compiling; template<> std::ostream& operator<<
I executed this code after compiling in codeblocks:- #include <stdio.h> int main() { char
This code is not compiling in my system; I'm using Eclipse. // Linked list
My compiler, MPLAB C30 (GCC v3.23) is not compiling this code: if(font_info.flags & FONT_LOWERCASE_ONLY)
I had this code from a previous question, but its not compiling: public interface
Following code is not compiling, can anybody please help what is wrong here class
Why is this C# code not compiling? public static Dictionary<short, MemoryBuffer> GetBulkCustom(int bufferId, int
Why does this code not print an exception stack trace? public class Playground {

Explore

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

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.