31

Apologies for the code dump:

gameObject.cpp:

#include "gameObject.h"
class gameObject
{
    private:
    int x;
    int y;
    public:
    gameObject()
    {
    x = 0;
    y = 0;
    }

    gameObject(int inx, int iny)
    {
        x = inx;
        y = iny;
    }

    ~gameObject()
    {
    //
    }
    int add()
    {
        return x+y;
    }
};

gameObject.h:

class gameObject
{
    private:
    int x;
    int y;
    public:
    gameObject();

    gameObject(int inx, int iny);
    ~gameObject();
    int add();
};

Errors:

||=== terrac, Debug ===|
C:\terrac\gameObject.cpp|4|error: redefinition of `class gameObject'|
C:\terrac\gameObject.h|3|error: previous definition of `class gameObject'|
||=== Build finished: 2 errors, 0 warnings ===|

I can't figure out what's wrong. Help?

sellibitze
  • 27,611
  • 3
  • 75
  • 95
Dataflashsabot
  • 1,369
  • 5
  • 19
  • 24

9 Answers9

58

You're defining the class in the header file, include the header file into a *.cpp file and define the class a second time because the first definition is dragged into the translation unit by the header file. But only one gameObject class definition is allowed per translation unit.

You actually don't need to define the class a second time just to implement the functions. Implement the functions like this:

#include "gameObject.h"

gameObject::gameObject(int inx, int iny)
{
    x = inx;
    y = iny;
}

int gameObject::add()
{
    return x+y;
}

etc

sellibitze
  • 27,611
  • 3
  • 75
  • 95
20

add in header files

#pragma once
Helperors
  • 209
  • 2
  • 2
  • 3
    What will this achieve? – IgorGanapolsky Jun 07 '16 at 15:53
  • @IgorGanapolsky `#pragma once` achieves the same thing as [zhangxiang's answer](https://stackoverflow.com/a/37145587/10001361) – SomeMosa Dec 28 '20 at 17:50
  • 2
    @IgorGanapolsky If you need to ```include``` two separate files, while those two ```include``` the same third file, then your compiler will give you an error because it will try to include the same file twice, using ```#pragma once``` stops the compiler from behaving like this, by simply not allowing it to ```include``` the same file twice, and use the once included file both times. – DaniyalAhmadSE Jun 04 '21 at 21:20
10

the implementation in the cpp file should be in the form

gameObject::gameObject()
    {
    x = 0;
    y = 0;
    }
gameObject::gameObject(int inx, int iny)
    {
        x = inx;
        y = iny;
    }

gameObject::~gameObject()
    {
    //
    }
int gameObject::add()
    {
        return x+y;
    }

not within a class gameObject { } definition block

frag
  • 415
  • 2
  • 6
  • 1
    In addition to this fix, constructors should use *ctor-initializer-list* and not the body of the constructor to initialize member variables. – Ben Voigt Sep 19 '10 at 16:36
  • absolutely agree (though not related to the question)(+1 from me) – frag Sep 19 '10 at 16:48
9

You should wrap the .h file like so:

#ifndef Included_NameModel_H

#define Included_NameModel_H

// Existing code goes here

#endif
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
zhangxiang
  • 147
  • 1
  • 1
  • Also you can ignore .h files, and place everything inside a .cpp file, with that #ifndef command. – morteza Oct 31 '22 at 16:24
5

You're defining the same class twice is why.

If your intent is to implement the methods in the CPP file then do so something like this:

gameObject::gameObject()
{
    x = 0;
    y = 0;
}
gameObject::~gameObject()
{
    //
}
int gameObject::add()
{
        return x+y;
}
locka
  • 5,809
  • 3
  • 33
  • 38
3

If you are having issues with templates or you are calling the class from another .cpp file

try using '#pragma once' in your header file.

Noel Shere
  • 39
  • 1
  • This was already posted as an answer to the same question [here](https://stackoverflow.com/a/34873122/3367799). – JJJ Mar 15 '19 at 04:05
1

Either try adding #pragma once at the top of your file, or the old way... Place this at the top of your code

#ifndef GAME_H //ensuring that this object is only initialized once
#define GAME_H 

and this below the last line

#endif

Which will ensure only a single initialization of the class.

A P
  • 2,131
  • 2
  • 24
  • 36
0

Include a few #ifndef name #define name #endif preprocessor that should solve your problem. The issue is it going from the header to the function then back to the header so it is redefining the class with all the preprocessor(#include) multiple times.

J.J.
  • 75
  • 8
-1

You define the class gameObject in both your .cpp file and your .h file.
That is creating a redefinition error.

You should define the class, ONCE, in ONE place. (convention says the definition is in the .h, and all the implementation is in the .cpp)

Please help us understand better, what part of the error message did you have trouble with?

The first part of the error says the class has been redefined in gameObject.cpp
The second part of the error says the previous definition is in gameObject.h.

How much clearer could the message be?

abelenky
  • 63,815
  • 23
  • 109
  • 159