为什么我会收到类错误的重新定义?

发布于 2024-09-24 05:25:03 字数 860 浏览 5 评论 0原文

对代码转储表示歉意:

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();
};

错误:

||=== 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 ===|

我不知道出了什么问题。帮助?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

错々过的事 2024-10-01 05:25:03

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

实际上,您不需要再次定义该类来实现这些功能。实现这样的功能:

#include "gameObject.h"

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

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

等等

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

菊凝晚露 2024-10-01 05:25:03

添加头文件

#pragma once

add in header files

#pragma once
空城缀染半城烟沙 2024-10-01 05:25:03

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;
    }

不在 a 内的 形式
游戏对象类
{
}
定义块

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

酸甜透明夹心 2024-10-01 05:25:03

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

#ifndef Included_NameModel_H

#define Included_NameModel_H

// Existing code goes here

#endif

You should wrap the .h file like so:

#ifndef Included_NameModel_H

#define Included_NameModel_H

// Existing code goes here

#endif
忘年祭陌 2024-10-01 05:25:03

你定义同一个类两次就是原因。

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

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

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;
}
一百个冬季 2024-10-01 05:25:03

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

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

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.

那片花海 2024-10-01 05:25:03

尝试在文件顶部添加 #pragma Once ,或者使用旧方法...将其放在代码顶部,

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

并将其置于最后一行下方,

#endif

这将确保仅对班级。

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.

末骤雨初歇 2024-10-01 05:25:03

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

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.

愿得七秒忆 2024-10-01 05:25:03

您可以在 .cpp 文件和 .h 文件中定义 class gameObject
这会产生重新定义错误。

您应该在 ONE 位置定义该类,ONCE
(按照惯例,定义在.h中,所有实现都在.cpp中)

请帮助我们更好地理解,您有错误消息的哪一部分有麻烦吗?

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

信息可以清楚多少?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文