I’m almost 100% sure I have the syntax right in both of these classes, however I’m getting the following errors:
For CShape.cpp – “error C2011: ‘CShape’ : ‘class’ type redefinition”
For CCircle.cpp – “error CS2504: ‘CShape’: base class undefined”
Here is the full code for CShape.cpp
#include <iostream>
using namespace std;
class CShape
{
protected:
float area;
virtual void calcArea();
public:
float getArea()
{
return area;
}
}
And here is the code for CCircle.cpp
#include <iostream>
#include "CShape.cpp"
#define _USE_MATH_DEFINES
#include "math.h"
using namespace std;
class CCircle : public CShape
{
protected:
int centerX;
int centerY;
float radius;
void calcArea()
{
area = M_PI * (radius * radius);
}
public:
CCircle(int pCenterX, int pCenterY, float pRadius)
{
centerX = pCenterX;
centerY = pCenterY;
radius = pRadius;
}
float getRadius()
{
return radius;
}
}
As you can see, CShape is the base class that CCircle is suppsoed to inherit from. I’m pretty new to C++, so I could have the file structures wrong (maybe the base is supposed to be in a header file?), if something like that matters.
Never #include .cpp files; that will lead to the kind of redefinition errors you are getting. Instead, declare the class in a header file and #include that one, and define the class methods in a .cpp file.
.cpp file:
You should split up CCircle similarly – and CCircle.h should #include CShape.h, and CCircle.cpp should #include CCircle.h.