• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
hkBattousai

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

6 posts in this topic

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.)
0

Share this post


Link to post
Share on other sites
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

0

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.)

0

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.
0

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?
0

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
0

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.
0

Share this post


Link to post
Share on other sites

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  
Followers 0