C++“类”类型重新定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21354132/
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
C++ 'class' type redefinition
提问by Tom smith
I have been attempting to work with classes in c++ for the first time. My circle class and associated header file were working fine, I then moved some files and since then keep getting an error which i have displayed below.
我第一次尝试在 C++ 中使用类。我的圆类和相关的头文件工作正常,然后我移动了一些文件,从那以后不断收到我在下面显示的错误。
c:\circleobje.cpp(3): error C2011: 'CircleObje' : 'class' type redefinition
c:\circleobje.h(4) : see declaration of 'CircleObje'
CircleObje.h
圆对象.h
#ifndef CircleObje_H
#define CircleObje_H
class CircleObje
{
public:
void setCol(float r, float g, float b);
void setCoord(int x, int y);
float getR();
float getG();
float getB();
int getX();
int getY();
};
#endif
CircleObje.cpp
圆对象.cpp
#include "CircleObje.h"
class CircleObje {
float rVal, gVal, bVal;
int xCor, yCor;
public:
void setCol(float r, float g, float b)
{
rVal = r;
gVal = g;
bVal = b;
}
void setCoord(int x, int y)
{
xCor = x;
yCor = y;
}
...
};
I haven't copied all of the .cpp functions as I didn't think they were relevant. These files were working without issue before I moved the file locations. Even after renaming them I still have the same error as above. Any ideas to solve the problem?
我没有复制所有的 .cpp 函数,因为我认为它们不相关。在我移动文件位置之前,这些文件工作正常。即使重命名它们后,我仍然有与上述相同的错误。任何解决问题的想法?
回答by pippin1289
The issue is that you are defining the class twice just as the compiler is telling you. In the cpp you should provide the definitions of the functions like so:
问题是,正如编译器告诉你的那样,你定义了两次类。在 cpp 中,您应该提供函数的定义,如下所示:
MyClass::MyClass() {
//my constructor
}
or
或者
void MyClass::foo() {
//foos implementation
}
so your cpp should look like:
所以你的 cpp 应该是这样的:
void CirleObje::setCol(float r, float g, float b)
{
rVal = r;
gVal = g;
bVal = b;
}
void CircleObje::setCoord(int x, int y)
{
xCor = x;
yCor = y;
}
...
And all the class variables should be defined in the .h file inside of your class.
并且所有类变量都应该在类内的 .h 文件中定义。
回答by java seeker
you are declaring your class multiple times once in header file and another in .cpp file which is redefining your class.
你在头文件中多次声明你的类,另一个是在重新定义你的类的 .cpp 文件中。
CircleObje.h
#ifndef CircleObje_H
#define CircleObje_H
class CircleObje
{
public:
void setCol(float r, float g, float b);
void setCoord(int x, int y);
float getR();
float getG();
float getB();
int getX();
int getY();
public:
float rVal, gVal, bVal;
int xCor, yCor;
};
#endif
CircleObje.cpp
#include "CircleObje.h"
void CircleObje::void setCol(float r, float g, float b)
{
rVal = r;
gVal = g;
bVal = b;
}
void CircleObje::setCoord(int x, int y)
{
xCor = x;
yCor = y;
}
回答by Manu343726
You have defined the class twice, in the header and in the cpp, so in the .cpp the compiler sees two definitions. Remove the definition of the class on the .cpp.
您已经在头文件和 cpp 中定义了该类两次,因此在 .cpp 中编译器会看到两个定义。删除 .cpp 上的类定义。
Class functions should be implemented in the cpp in this way:
类函数应该以这种方式在cpp中实现:
<return_type> <class_name>::<function_name>(<function_parameters>)
{
...
}
Consider this example class:
考虑这个示例类:
//foo.hpp
struct foo
{
int a;
void f();
}
The class is implemented in the foo.cpp
file:
该类在foo.cpp
文件中实现:
#include "foo.hpp"
void foo::f()
{
//Do something...
}
回答by Gabriel L.
Remove class CircleObje {
, public
and the ending bracket };
and it should work. You already defined your class in the .H, thus no need to redefine it in the CPP.
删除class CircleObje {
,public
和结束括号};
,它应该可以工作。您已经在 .H 中定义了您的类,因此无需在 CPP 中重新定义它。
Also, you should write your member implementation (in CPP file) like this :
此外,您应该像这样编写您的成员实现(在 CPP 文件中):
float CircleObje::getR() { /* your code */ }