C ++ class type override

I am trying to work with classes in C ++ for the first time. My environment class and the associated header file worked fine, I then moved some files and since then I continue to receive the error that I displayed below.

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

c:\circleobje.h(4) : see declaration of 'CircleObje'

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

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

...
};

I did not copy all the .cpp functions, as I did not think they were relevant. These files worked without problems before I moved the files. Even after renaming, I still have the same error as above. Any ideas to solve the problem?

+4
source share
4 answers

The problem is that you define the class twice, as the compiler tells you. In cpp, you must specify the definitions of such functions:

MyClass::MyClass() {
  //my constructor
}

or

void MyClass::foo() {
   //foos implementation
}

so your cpp should look like this:

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

...

.h .

+4

, cpp, .cpp . .cpp.

cpp :

<return_type> <class_name>::<function_name>(<function_parameters>)
{
    ...
}

:

//foo.hpp

struct foo
{
    int a;

    void f();
}

foo.cpp:

#include "foo.hpp"

void foo::f()
{
    //Do something...
}
+1

, - .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;
}
+1

class CircleObje {, public };, . .H, CPP.

, ( CPP) :

float CircleObje::getR() { /* your code */ } 
0

Source: https://habr.com/ru/post/1523708/


All Articles