7 Replies - 561 Views - Last Post: 01 May 2013 - 04:59 AM Rate Topic: -----

#1 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

including object of one class in another

Posted 25 April 2013 - 04:44 AM

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 ??
Is This A Good Question/Topic? 0
  • +

Replies To: including object of one class in another

#2 frostraver  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 93
  • Joined: 11-September 12

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.

This post has been edited by frostraver: 25 April 2013 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#3 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

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
Was This Post Helpful? 0
  • +
  • -

#4 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

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:
#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

Was This Post Helpful? 0
  • +
  • -

#5 frostraver  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 93
  • Joined: 11-September 12

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?
Was This Post Helpful? 0
  • +
  • -

#6 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

Re: including object of one class in another

Posted 26 April 2013 - 03:51 AM

it worked .. thanks fr you help :)
Was This Post Helpful? 0
  • +
  • -

#7 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

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:

#ifndef _WHATEVER_
#define _WHATEVER_

// Declarations go here.

#endif



Simple as that.

This post has been edited by ButchDean: 27 April 2013 - 10:34 PM

Was This Post Helpful? 0
  • +
  • -

#8 cbrawl  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 19-December 10

Re: including object of one class in another

Posted 01 May 2013 - 04:59 AM

View PostB0rn2c0de, 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1