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 9094075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:15:47+00:00 2026-06-16T23:15:47+00:00

I am trying to share info between classes using information from here , except

  • 0

I am trying to share info between classes using information from here,
except that I am not able to get these two classes to compile and thus to
communicate with each other.

So, what I want is for ClassB to get a reference to ClassA, and
ClassB to be initialized in ClassA.

Compiler errors (using g++):

In member function 'void ClassA::foo()':
hello.cpp:9: error: 'ClassB' was not declared in this scope
hello.cpp:9: error: 'someB' was not declared in this scope
hello.cpp:9: error: expected type-specifier before 'ClassB'
hello.cpp:9: error: expected ';' before 'ClassB'

So then I try adding class ClassB; before ClassA,

like:

class ClassB; // just this here.

class ClassA; // all of classA here.

class ClassB; // all of classB here.

And I get:

hello.cpp: In member function ‘void ClassA::foo()’:
hello.cpp:11: error: invalid use of incomplete type ‘struct ClassB’
hello.cpp:3: error: forward declaration of ‘struct ClassB’
hello.cpp:12: error: invalid use of incomplete type ‘struct ClassB’
hello.cpp:3: error: forward declaration of ‘struct ClassB’


This is the code that am I trying to use based on the website above:

include stdio.h

class ClassA {
    public:
        void foo() {
            ClassB *someB = new ClassB();
            someB->setRelative(this);
        }

        void bar() {
            printf("B is communicating with me =)\n");
        }
};

class ClassB {

    ClassA *m_relative;

    public:
        void setRelative(ClassA *other) {
            this->m_relative = other;
        }

        void foo() {
            m_relative->bar();
        }
};
  • 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-06-16T23:15:48+00:00Added an answer on June 16, 2026 at 11:15 pm

    You’re obviously having a circular dependency here, something you can’t really solve using headers only (might be possible with some macro tricks, but I wouldn’t worry about).

    What you should do is splitting your code into declarations (headers) and implementations.

    a.h

    // only include this file once per translation unit
    #pragma once
    
    // tell the compiler B is going to be a class,
    // so B* is a pointer to a class - nothing more is needed for now.
    class B;
    
    // full declaration of A
    class A {
        B *child;
    
    public:
        A(void);
    }
    

    a.cpp

    #include "a.h" // included whenever the structure/size of A has to be known
    #include "b.h" // same for B
    
    A::A(void) : child(new B(this)) {
        // do something with child or...
        B * = new B(this);
    }
    

    b.h

    // only include this file once per translation unit
    #pragma once
    
    // again, just tell the compiler A is going to be a class
    class A;
    
    class B {
        A *parent;
    public:
        B(A *pparent);
    }
    

    b.cpp

    #include "a.h"
    #include "b.h"
    
    B::B(A *pparent) : parent(pparent) {
        // do something with parent
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to download share data from a stock exchange using python. The problem
I'm trying to get just a piece of information from Facebook, but I don't
I am trying to share plain text while using a Share Action Provider via
I am trying to share two different using one shared memory block using the
I'm trying to share some text using an intent: Intent i = new Intent(android.content.Intent.ACTION_SEND);
I'm trying to add some share this javascript in between the head tags of
We're trying to add Facebook Share & Like to an iOS game that we
I am trying to share Spring object definitions between a web application, unit testing,
Pretty new to threading and I have this QList that the threads share between
What I'm trying to do is very similar to what make-local-variable does except that

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.