C++ 跨多个文件的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11812014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:34:29  来源:igfitidea点击:

Classes across multiple files in C++

c++

提问by NealR

I'm almost 100% sure I have the syntax right in both of these classes, however I'm getting the following errors:

我几乎 100% 确定我在这两个类中都有正确的语法,但是我收到以下错误:

For CShape.cpp - "error C2011: 'CShape' : 'class' type redefinition" For CCircle.cpp - "error CS2504: 'CShape': base class undefined"

对于 CShape.cpp -“错误 C2011:'CShape':'class' 类型重新定义”对于 CCircle.cpp -“错误 CS2504:'CShape':基类未定义”

Here is the full code for CShape.cpp

这是 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

这是 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.

如您所见,CShape 是 CCircle 应该继承的基类。我对 C++ 很陌生,所以我可能会弄错文件结构(也许基础应该在头文件中?),如果这样的话。

回答by Aasmund Eldhuset

Never #include .cpp files; that will lead to the kind of redefinition errors you are getting. Instead, declarethe class in a header file and #include that one, and definethe class methods in a .cpp file.

从不#include .cpp 文件;这将导致您遇到的那种重新定义错误。相反,在头文件中声明类并 #include 那个,并在 .cpp 文件中定义类方法。

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

.cpp file:

.cpp 文件:

// 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.

您应该类似地拆分 CCircle - CCircle.h 应该 #include CShape.h,而 CCircle.cpp 应该 #include CCircle.h。

回答by π?ντα ?ε?

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.

正如您所猜想的那样,您应该在单独的文件中组织您的类以进行声明(头文件)和定义(.cpp 文件)。您可以在头文件中将成员函数定义(带主体)保留为(建议)内联。将适当的包含阻止程序放入您的头文件中,以避免多个类声明。

CShape.h:

CShape.h:

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

CShape.cpp:

CShape.cpp:

#include "CShape.h"

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

CCircle.h:

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:

CCircle.cpp:

#include "CCircle.h"

CCircle::CCircle(int pCenterX, int pCenterY, float pRadius)
: centerX(pCenterX)
, centerY(pCenterY)
, radius(pRadius)
{
}