C++'class'类型重定义

Tom*_*ith 9 c++ class

我第一次尝试使用c ++中的类.我的圈子类和相关的头文件工作正常,然后我移动了一些文件,然后继续得到我在下面显示的错误.

c:\circleobje.cpp(3): error C2011: 'CircleObje' : 'class' type redefinition

c:\circleobje.h(4) : see declaration of 'CircleObje'
Run Code Online (Sandbox Code Playgroud)

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

#endif
Run Code Online (Sandbox Code Playgroud)

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

...
};
Run Code Online (Sandbox Code Playgroud)

我没有复制所有.cpp函数,因为我认为它们不相关.在移动文件位置之前,这些文件没有问题.即使重命名后我仍然有与上面相同的错误.有什么想法来解决这个问题吗?

pip*_*289 9

问题是你正在编译器告诉你两次定义类.在cpp中,您应该提供函数的定义,如下所示:

MyClass::MyClass() {
  //my constructor
}
Run Code Online (Sandbox Code Playgroud)

要么

void MyClass::foo() {
   //foos implementation
}
Run Code Online (Sandbox Code Playgroud)

所以你的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;
}

...
Run Code Online (Sandbox Code Playgroud)

并且所有类变量都应该在类的.h文件中定义.