• Advertisement
Sign in to follow this  

[C++] Calling member of another class in another file

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

I have two classes, namely "A" and "B". I want to store pointer of an A object inside a B object and call members of A object from B object. Here is my code: main.cpp:
#include <iostream>
#include "A.h"
#include "B.h"

using namespace std;

int main()
{
	int num = 10;
	int dbl_num;
	
	A * my_a;
	B * my_b;		// line #13
	b->setA(my_a);		// line #14
	
	dbl_num = b->callMemberOfAObject(num);
	
    cout << "num      : " << num     << endl;
    cout << "dbld_num : " << dbl_num << endl << endl;
    
    system("pause");
    return 0;
}
A.h:
#ifndef _A_H_
#define _A_H_

#include "B.h"

class A
{
	public:
	int multiplyByTwo(int num)
	{
		int ret;
		ret = 2 * num;
		return ret;
	}
};

#endif	// #ifndef _A_H_

B.h:
#ifndef _B_H_
#define _B_H_

#include "A.h"

class B
{
	private:
	A * a;					// line #9
	public:
	int callMemberOfAObject(int num)
	{
		int ret;
		ret = a->multiplyByTwo(num);	// line #14
		return ret;
	}
	void setA(A * a)			// line #17
	{					// line #18
		this->a = a;			// line #19
	}
};

#endif	// #ifndef _B_H_
When I build this code, compiler gives the following error messages:
Quote:
B.h|9|error: ISO C++ forbids declaration of `A' with no type| B.h|9|error: expected `;' before '*' token| B.h|17|error: `A' has not been declared| B.h|18|error: ISO C++ forbids declaration of `a' with no type| B.h||In member function `int B::callMemberOfAObject(int)':| B.h|14|error: `a' was not declared in this scope| B.h|14|warning: unused variable 'a'| B.h||In member function `void B::setA(int*)':| B.h|19|error: 'class B' has no member named 'a'| main.cpp||In function `int main()':| main.cpp|14|error: `b' was not declared in this scope| main.cpp|13|warning: unused variable 'my_b'| ||=== Build finished: 7 errors, 2 warnings ===|
******************** HOWEVER, I don't get any error message and everything works fine when I run the following single-file code:
#include <iostream>

using namespace std;

class A
{
	public:
	int multiplyByTwo(int num)
	{
		int ret;
		ret = 2 * num;
		return ret;
	}
};

class B
{
	private:
	A * a;
	public:
	int callMemberOfAObject(int num)
	{
		int ret;
		ret = a->multiplyByTwo(num);
		return ret;
	}
	void setA(A * a)
	{
		this->a = a;
	}
};

int main()
{
	int num = 10;
	int dbl_num;
	
	A * my_a;
	B * my_b;
	my_b->setA(my_a);
		dbl_num = my_b->callMemberOfAObject(num);
	
    cout << "num      : " << num     << endl;
    cout << "dbld_num : " << dbl_num << endl << endl;
    
    system("pause");
    return 0;
}
Can anyone tell me, why the multi-file version of my code does not work, while single-file version works without any error messages, nor even any warning? I'm working on a project with lots of code, so I have to divide my code into files. I need a solution for this. Any help will be appreciated. (IDE: Tried with both Visual Studio 2005 and Code::Blocks. They give the same kind of error messages.)

Share this post


Link to post
Share on other sites
Advertisement
you need to forward declare class A in B.h and include A.h in B.cpp instead of B.h


//B.h

class A;

class B
{

};




//B.cpp

include "A.h"

//stuff

Share this post


Link to post
Share on other sites
Quote:
Original post by owl
you need to forward declare class A in B.h

I did so, and I got these two errors:
Quote:
1) B.h|16|error: invalid use of undefined type `struct A'|
at line: "ret = a->multiplyByTwo(num);"

2) B.h|6|error: forward declaration of `struct A'|
at forward declaration of A class.



Quote:
Original post by owl
... and include A.h in B.cpp instead of B.h

My B.cpp is empty, do I need to include A.h in B.cpp. I did so, and included, but nothing has changed.


(Note: Used IDE Code::Blocks 8.02 to test this code.)

Share this post


Link to post
Share on other sites
Quote:
Original post by Battousai
Quote:
Original post by owl
you need to forward declare class A in B.h

I did so, and I got these two errors:
Quote:
1) B.h|16|error: invalid use of undefined type `struct A'|
at line: "ret = a->multiplyByTwo(num);"

2) B.h|6|error: forward declaration of `struct A'|
at forward declaration of A class.




You don't need to include A.h in main.cpp if you already include it in B.h

Try moving your methods implementation to the cpp files.

Share this post


Link to post
Share on other sites
Quote:
Original post by owl
You don't need to include A.h in main.cpp if you already include it in B.h

Try moving your methods implementation to the cpp files.


Ohhh, that worked! I don't believe it, I wasted my hours on it, was that so simple??

But still I don't understand the logic behind this. Why did I have to forward declare "class A" in "B.h"? "B.h" had already included "A.h", when it includes "A.h", the class "A" would automatically be declared in "B.h", why declare it for a second time?

Share this post


Link to post
Share on other sites
If you include A.h in B.h and B.h in A.h, then A.h gets included in itself through B.h and the universe asplodes.

I didn't read you original post completely, I just took some wild guesses. lol

Share this post


Link to post
Share on other sites
Quote:
Original post by owl
If you include A.h in B.h and B.h in A.h, then A.h gets included in itself through B.h and the universe asplodes.

I didn't read you original post completely, I just took some wild guesses. lol


That's right... :)
Thank you in advance.

Share this post


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

  • Advertisement