Why am I getting this redefinition of class error?

2019-04-04 02:35发布

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?

8条回答
▲ chillily
2楼-- · 2019-04-04 03:19

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.

查看更多
再贱就再见
3楼-- · 2019-04-04 03:21

You should wrap the .h file like so:

#ifndef Included_NameModel_H

#define Included_NameModel_H

// Existing code goes here

#endif
查看更多
登录 后发表回答