为什么我会收到类错误的重新定义?
对代码转储表示歉意:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您在头文件中定义类,将头文件包含到 *.cpp 文件中,然后再次定义该类,因为第一个定义被头文件拖到翻译单元中。但每个翻译单元只允许有一个游戏对象类定义。
实际上,您不需要再次定义该类来实现这些功能。实现这样的功能:
等等
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:
etc
添加头文件
add in header files
cpp 文件中的实现应该采用
不在 a 内的 形式
游戏对象类
{
}
定义块
the implementation in the cpp file should be in the form
not within a
class gameObject
{
}
definition block
您应该像这样包装
.h
文件:You should wrap the
.h
file like so:你定义同一个类两次就是原因。
如果您的目的是实现 CPP 文件中的方法,请执行以下操作:
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 文件调用该类,
请尝试在头文件中使用“#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.
尝试在文件顶部添加
#pragma Once
,或者使用旧方法...将其放在代码顶部,并将其置于最后一行下方,
这将确保仅对班级。
Either try adding
#pragma once
at the top of your file, or the old way... Place this at the top of your codeand this below the last line
Which will ensure only a single initialization of the class.
包含一些应该可以解决您的问题的 #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.
您可以在
.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?