Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Classes across multiple files in C++

Tags:

c++

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.

like image 275
NealR Avatar asked Aug 04 '12 21:08

NealR


People also ask

Can a header file have multiple classes?

There is no obligation regarding header and source files. In particular (contrarily to Java) you can (and often want) to have several declarations in a header file, and several class and function definitions (& implementation) in a source file.

Can we have multiple classes in same C++ file?

In C++ you can define multiple classes inside of a single file. You will not run into trouble by doing it. Would you elaborate "You will not run into trouble"? Your answer is opposite to the others.

Which two files are usually defined for a class?

C++ classes (and often function prototypes) are normally split up into two files. The header file has the extension of . h and contains class definitions and functions. The implementation of the class goes into the .


2 Answers

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.

// CShape.h
class CShape
{
protected:
    float area;
    virtual void calcArea();
public:
    float getArea();
}

.cpp file:

// CShape.cpp
#include "CShape.h"
#include <iostream>
using namespace std;

float CShape::getArea() {
    return area;
}

You should split up CCircle similarly - and CCircle.h should #include CShape.h, and CCircle.cpp should #include CCircle.h.

like image 120
Aasmund Eldhuset Avatar answered Oct 22 '22 07:10

Aasmund Eldhuset


As you guessed, you should organize your classes in separate files for declaration (header file) and definition (.cpp file). You may leave member function definitions (with body) as (suggested) inline in the header files. Put appropriate include blockers into your header files, to avoid multiple class declarations.

CShape.h:

#ifndef __CSHAPE_H__
#define __CSHAPE_H__
class CShape
{
protected:
    float area;
    virtual void calcArea();
public:
    float getArea()
    {
        return area;
    }
};
#endif

CShape.cpp:

#include "CShape.h"

void CShape::calcArea()
{
    // Your implementation
}

CCircle.h:

#ifndef __CCIRCLE_H__
#define __CCIRCLE_H__
#include "CShape.h"

class CCircle : public CShape
{
protected:
    int centerX;
    int centerY;
    float radius;
    virtual void calcArea();
    {
        area = M_PI * (radius * radius);
    }
public:
     CCircle(int pCenterX, int pCenterY, float pRadius);
     inline float getRadius()
     {
         return radius;
     }
};
#endif

CCircle.cpp:

#include "CCircle.h"

CCircle::CCircle(int pCenterX, int pCenterY, float pRadius)
: centerX(pCenterX)
, centerY(pCenterY)
, radius(pRadius)
{
}
like image 5
πάντα ῥεῖ Avatar answered Oct 22 '22 06:10

πάντα ῥεῖ