C++ 为什么我会重新定义类错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3746484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:36:27  来源:igfitidea点击:

Why am I getting this redefinition of class error?

c++classredefinition

提问by Dataflashsabot

Apologies for the code dump:

为代码转储道歉:

gameObject.cpp:

游戏对象.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:

游戏对象.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?

我无法弄清楚出了什么问题。帮助?

回答by sellibitze

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.

您正在头文件中定义类,将头文件包含到 *.cpp 文件中并再次定义类,因为第一个定义被头文件拖入翻译单元。但是每个翻译单元只允许一个游戏对象类定义。

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

等等

回答by frag

the implementation in the cpp file should be in the form

cpp文件中的实现应该是这样的形式

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

不在类 gameObject { } 定义块中

回答by locka

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:

如果您的目的是实现 CPP 文件中的方法,请执行以下操作:

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

回答by Helperors

add in header files

添加头文件

#pragma once

回答by zhangxiang

You should wrap the .hfile like so:

您应该.h像这样包装文件:

#ifndef Included_NameModel_H

#define Included_NameModel_H

// Existing code goes here

#endif

回答by Noel Shere

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

如果您遇到模板问题或者您正在从另一个 .cpp 文件调用该类

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

尝试在头文件中使用“#pragma once”。

回答by J.J.

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.

包括一些应该可以解决您的问题的 #ifndef name #define name #endif 预处理器。问题是它从头到函数然后返回到头,因此它使用所有预处理器(#include)多次重新定义类。

回答by abelenky

You define the class gameObjectin both your .cppfile and your .hfile.
That is creating a redefinition error.

class gameObject.cpp文件和.h文件中都定义了。
这会造成重新定义错误。

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

您应该在一个地方定义类ONCE。(约定说定义在 中,所有实现都在 中).h.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.

错误的第一部分表示该类已在 中重新定义gameObject.cpp
错误的第二部分表示先前的定义在 中gameObject.h

How much clearer could the message be?

信息能清晰到什么程度?