Sign in to follow this  

2 classes that are dependant on each other

This topic is 3487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As is, that is impossible. In order to create an instance of A, one needs a B instance, and to make a B an A needs to exist.

Without the actual context (which is typically important, most such problems can be designed around) it is hard to give any concrete advice. For example, instead of B having an A as a member, it might be possible to pass an A instance to member functions of B that require an A. Or, it might be possible to use a reseatable reference, like a (smart) pointer, on one side of the relationship.

Share this post


Link to post
Share on other sites
what if only one of the classes above needed to be passed in the other class through the constructor.

like this

class A
{
public:
A();
~A();
SomeFunc()
{
//create instance of class B and do stuff
}
};

class B
{
public:
B(A);
~B();
};



i think i still have the same problem

Share this post


Link to post
Share on other sites
Quote:
Original post by b1gjo3
i think i still have the same problem


No, A can use the this pointer to create B:


void A::SomeFunc() {
B(*this);
//...
}

Share this post


Link to post
Share on other sites
just one extra point - unless you want B to have a copy of A then

B(const A&) or B(A&) would make more sense. And then you could call


void A::SomeFunc()
{
B(this);
//...
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
Quote:
Original post by b1gjo3
i think i still have the same problem


No, A can use the this pointer to create B:

*** Source Snippet Removed ***


when i do this, i get an error saying the class has no constructors

Share this post


Link to post
Share on other sites
Post the actual errors.

If you have changed the code quite a bit, post the updated code.

This works for me:

class A
{
public:
A(int value) : value(value) {}

void SomeFunc();

int value;
};

class B
{
public:
B(const A &a) : a(a) {}

void foo()
{
std::cout << a.value << std::endl;
}
private:
A a;
};

void A::SomeFunc()
{
B b(*this);
b.foo();
}


int main()
{
A a(42);
a.SomeFunc();
}

Share this post


Link to post
Share on other sites
Your code snippet only contains constructor/destructor declarations. Did you give them a definition as well?

Here's an example definition for A's constructor:


A::A()
{
cout << "I'm A's constructor!" < endl;
}



If that's not it, post the exact error you are getting.

Share this post


Link to post
Share on other sites
Without knowing what you intend on doing with A and B the easy solution is to create a thrid class C. And make C the base class that A and B derive from.

Class C
{
C();
};

Class A : public C
{
A(C &c);
};

Class B : public C
{
B(C &c);
};


And make sure that whatever fields you need from A in B.. and from B in A.. exist within C. Thus you can generate a Class C, and use that to generate A and B as necessary. Also, A and B both do not require eachother to build or compile, and they'll have a common naming scheme for all shared data (represented by class C).

Hope that helps.

Share this post


Link to post
Share on other sites
in this class, class A is EdsCam, class B is EVFView

my forward declaration

class EVFView;




here is the "SomeFunc"

void EdsCam::_ShowEVFWindow()
{
EVFView* liveView = new EVFView(this);
}




here is class B which is EVFView

class EVFView : public Window
{
public:
EVFView(EdsCam* cam);
//..

EVFView::EVFView(EdsCam* cam):Window("Live View", 500, 410)
{
this->cam = cam;
SetupWndClass(LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_BIG)), LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_SM)));
InitWindow();
}




errors are
edscam.hpp(236) : error C2514: 'EVFView' : class has no constructors
edscam.hpp(11) : see declaration of 'EVFView'

it looks like my forward declaration is not right, what would it be?

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Post the actual errors.

If you have changed the code quite a bit, post the updated code.

This works for me:
*** Source Snippet Removed ***


thats exactly what i need, except that both classes need to have private member of each other.

Share this post


Link to post
Share on other sites
EdsCam is a class that can control all the properties of the EDS series of Canon digital cameras.

EVFView is a window that will be used to show live view images from the certain EdsCam object.

Share this post


Link to post
Share on other sites
Quote:
Original post by b1gjo3
EdsCam is a class that can control all the properties of the EDS series of Canon digital cameras.

EVFView is a window that will be used to show live view images from the certain EdsCam object.

So, you need one EdsCam per physical camera, and you need one EVFView per window on the screen. A camera cares not about the windows on the screen, so the AdsCam does not need to know about te EVFView class.

I suspect you need something more akin to this.

class EdsCam
{
// ...
};

class EVFView
{
public:
EVFView(EdsCam& camera)
: m_camera(camera)
{
// ...
}

private:
EdsCam& m_camera;
};

int main(int, char*[])
{
EdsCam camera;
EVFView view(camera);
// ...
}

Share this post


Link to post
Share on other sites
The file that contains the function definitions for EdsCam needs to include the header file that contains the declaration for EVFView.

A forward declaration only allows you to create a pointer or a reference to the class that you forward declare. If you need to create instances of it or make function calls to it, you need to include it's header file.

Share this post


Link to post
Share on other sites

This topic is 3487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this