hey guys
i was creating an air hockey type game in which i have 2 classes
1)pbat.h
2)ball.h
to create the ai function of pbat i require certain parameters of the ball object so i included ball.h in pbat.h but when i do so there are 50 something errors the main error is
error C2011: 'ball' : 'class' type redefinition
why is this happening ??
including object of one class in another
Page 1 of 17 Replies - 1247 Views - Last Post: 01 May 2013 - 04:59 AM
Replies To: including object of one class in another
#2
Re: including object of one class in another
Posted 25 April 2013 - 01:28 PM
This error means that you are already including the other other class in you file.
I gues you're using .cpp files and .h files?
If this is the fact you can use #include "ball.h" in the .cpp file of the other class and then write "class ball" without the quotes on top of your .h file.
I hope this solves your problem.
Maybe post your code if it's not too long so I can take a look at it.
I gues you're using .cpp files and .h files?
If this is the fact you can use #include "ball.h" in the .cpp file of the other class and then write "class ball" without the quotes on top of your .h file.
I hope this solves your problem.
Maybe post your code if it's not too long so I can take a look at it.
This post has been edited by frostraver: 25 April 2013 - 01:28 PM
#3
Re: including object of one class in another
Posted 25 April 2013 - 02:21 PM
Quote
use #include "ball.h" in the .cpp file of the other class and then write "class ball" without the quotes on top of your .h file
I didnt quite get what u mean by that
And neways i didnt make a seperate .cpp for each header file i defined the functions in the .h file only
I dont quite understand why people do so is there a specific reason ??
And cant i include the other one .h into another without making a seperate .cpp ??
Thanks in advance
#4
Re: including object of one class in another
Posted 25 April 2013 - 09:58 PM
Hello,
This happens because you are including the same file twice, so really, it's an infinite loop of including pBat.h and ball.h. To prevent this, we need header inclusion guards. I've found an article here: Header Include guard
You can either have:
Where #ifndef, #define and #endif are what makes up this type of inclusion guard.
You also have #pragma once
#pragma Directive
#pragma once is the non-standard way while the other way is the standard way. Read up on some C++ Inclusion guard tutorials.
Hope this helps
/>/>
This happens because you are including the same file twice, so really, it's an infinite loop of including pBat.h and ball.h. To prevent this, we need header inclusion guards. I've found an article here: Header Include guard
You can either have:
#ifndef _MYINCGUARD_H #define _MYINCGUARD_H #include "pbat.h" #include "Ball.h" class MyClass : public BaseClase { public: MyClass(); //ctor ~MyClass(); //dtor private: int MyIntVar; }; #endif
Where #ifndef, #define and #endif are what makes up this type of inclusion guard.
You also have #pragma once
#pragma once #include "pbat.h" #include "Ball.h" class MyClass : public BaseClase { public: MyClass(); //ctor ~MyClass(); //dtor private: int MyIntVar; };
#pragma Directive
#pragma once is the non-standard way while the other way is the standard way. Read up on some C++ Inclusion guard tutorials.
Hope this helps

This post has been edited by aaron1178: 26 April 2013 - 12:51 AM
#5
Re: including object of one class in another
Posted 26 April 2013 - 01:08 AM
Hi,
why people do this is to keep their files a bit tidier. You can however, as you're doing, write everything in the header file though this is only done in a number of situations I can't quite recall.
One thing I know is that everything in the header file won't be readeable by someone else, where some things in the .cpp file won't be hidden.
I hope you figured your problem out by the prvious post?
why people do this is to keep their files a bit tidier. You can however, as you're doing, write everything in the header file though this is only done in a number of situations I can't quite recall.
One thing I know is that everything in the header file won't be readeable by someone else, where some things in the .cpp file won't be hidden.
I hope you figured your problem out by the prvious post?
#6
Re: including object of one class in another
Posted 26 April 2013 - 03:51 AM
it worked .. thanks fr you help

#7
Re: including object of one class in another
Posted 27 April 2013 - 10:29 PM
Don't use #pragma once, that is Microsoft-specific. Use include guards:
Simple as that.
#ifndef _WHATEVER_ #define _WHATEVER_ // Declarations go here. #endif
Simple as that.
This post has been edited by ButchDean: 27 April 2013 - 10:34 PM
#8
Re: including object of one class in another
Posted 01 May 2013 - 04:59 AM
B0rn2c0de, on 25 April 2013 - 04:21 PM, said:
Quote
use #include "ball.h" in the .cpp file of the other class and then write "class ball" without the quotes on top of your .h file
I didnt quite get what u mean by that
And neways i didnt make a seperate .cpp for each header file i defined the functions in the .h file only
I dont quite understand why people do so is there a specific reason ??
And cant i include the other one .h into another without making a seperate .cpp ??
Thanks in advance
for every class you should have a .cpp file as well as a header file because let's say you want to give that class to someone but don't want to show them your source. Your .h will show them the template of all the functions/variables and whatnot without showing them how those functions work. Other than that the only other reasons to have separate .cpp is to better organize all functions related to one class and not have a 10,000 line main.cpp.
So your project will involve 5 files:
main.cpp ball.h ball.cpp bat.h bat.cpp
Most IDEs will automatically make a class for you and already add all the incudes you need for them to work together and keep them all in the same folder as your main.cpp
Page 1 of 1