• Advertisement
Sign in to follow this  

C++ Class Question

This topic is 4434 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

class b;

class a
{
	public:
	
	b* first;
	
};

class b
{
	public:
	
	a ins;
};
this works but:
class x
{
public:
 float xi;
 
 x()
 {
 xi=2;
 }
};


class b;

class a
{
	public:
	
	b* first;
	
};

class b:public x
{
	public:
	
	a ins;
};

int main (int argc, char * const argv[]) {
	b test;
	cout << test.xi << endl;
	
	a test2;
	cout << test2.first.xi << endl;

    return 0;
}
i need this structure. how do i use a class prototype with a inherited class?

Share this post


Link to post
Share on other sites
Advertisement
What is the error you are getting? I am guessing it is:
request for member `xi' in `test2.a::first', which is of non-aggregate type `b*'

At least that is what g++ reports when I copy and paste your code. The fix here is that you are trying to use the wrong operator.

cout << test2.first.xi << endl;

In your class, the member variable 'first' is a pointer so you need to use -> like:

cout << test2.first->xi << endl;

This cleared up the compile errors ( at least in g++ ).

Note that this is bad because you are trying to use a pointer ( first ) that has not been created yet. My prof used to say this is like shooting into a house. You may miss most of the time but when you hit somebody it's really really bad.

Share this post


Link to post
Share on other sites

class x
{
public:
float xi;

x()
{
xi=2;
}
};


class b;

class a
{
public:

b* first;

a(){
first = new b();
}

};

class b:public x
{
public:

a ins;
};

int main (int argc, char * const argv[]) {
a test;
cout << test.first->xi << endl;

/*a test2;
cout << test2.first->x1 << endl;*/

return 0;
}


: forward declaring of b
: invalid use of b

i know this is really bad but i need it... :-(

	a test;
test.first = new b();
cout << test.first->xi << endl;
with this main it works!

in my project class a is the unit class and class b is the building class. units build houses and houses build units. these units/buildings are stored in vectors -> so i need this structure

other ideas?

Share this post


Link to post
Share on other sites
Seperate the definition of your constructor from the class definition. Put the definition of the constructor after all the class definitions.

Share this post


Link to post
Share on other sites
test2's member "first" is never initialized, and dereferencing it here is undefined behavior. try something like this:


#include <iostream>

class x
{
public:
float xi;

x()
{
xi=2;
}
};


class b;

class a
{
public:
b* first;
};

class b:public x
{
public:
b()
{
ins.first = this;
}

a ins;
};

int main (int argc, char * const argv[]) {
b test;
std::cout << test.xi << std::endl;
std::cout << &test << std::endl << test.ins.first << std::endl;

a test2;
test2.first = &test;

std::cout << test2.first->xi << std::endl;

return 0;
}

Share this post


Link to post
Share on other sites
If you haven't done so I would suggest drawing your class relationships out on paper first. Especially if you have classes that are linked so close together. You don't need formal methods like UML, just boxes with class names and lines to show how they relate to each other. You will often find that by adding a another class here or moving some things around your class relationships become a lot easier to understand and hence easier to code. I have a log book full of little box diagrams as I try to work out class relationships before I code them.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement