12

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:\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 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?

Tom smith
  • 670
  • 2
  • 15
  • 31
  • 4
    I suggest you [read some books about C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Jan 25 '14 at 17:58
  • Thanks, I will have a look through that list. Also, I can only accept one but thanks for the answers, they were all helpful. – Tom smith Jan 25 '14 at 18:55
  • sometimes it solve by using pre-processor directives i had same problem as you and i am aware of using namespace and class name before functions and constructors but i got the error again but when i use #pragma once in first line of header file errors will be disappear – Ehsan Panahi Mar 05 '19 at 07:03

5 Answers5

16

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:

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

or

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

so your cpp should look like:

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.

pippin1289
  • 4,861
  • 2
  • 22
  • 37
3

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.

Class functions should be implemented in the cpp in this way:

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

#include "foo.hpp"

void foo::f()
{
    //Do something...
}
Manu343726
  • 13,969
  • 4
  • 40
  • 75
1

you are declaring your class multiple times once in header file and another in .cpp file which is redefining your class.

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;
}
java seeker
  • 1,246
  • 10
  • 13
0

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.

Also, you should write your member implementation (in CPP file) like this :

float CircleObje::getR() { /* your code */ } 
Gabriel L.
  • 4,678
  • 5
  • 25
  • 34
0

You have to put #pragma once in the first line of the header file, then the errors will disappear.

avariant
  • 2,234
  • 5
  • 25
  • 33
  • The #ifndef / #define pair produces the exact same result as #pragma once, so that will not fix the issue. The problem is that the class is declared in both the .h and the .cpp files. – avariant Feb 28 '23 at 17:18