使用#include和包含保护的单独文件中的C++继承

RIB*_*dot 22 c++ inheritance include abstract-data-type

我是Stack Overflow的新手,我正在自学C++,但我仍然是一个初学者.在完成了我正在使用的一本书(可能被认为过时和/或不是一本好书)之后,我决定通过自己尝试来重新强化一些概念,仅在需要时引用该书,但我似乎被卡住了.我试图解决的概念是继承,多态,抽象数据类型(ADT),并将我的类的代码分成头文件(.h)和C++文件(.cpp).对于文本墙提前抱歉,我只想清楚明确地说明我需要的地方.

所以,我的目标是创建简单的形状类,在适用的情况下相互继承.我有四个类:myPoly,myRectangle,myTriangle和mySquare.myPoly,如果我理解这个概念,应该是一个ADT,因为其中一个方法是一个纯虚函数(区域方法),因为创建一个myPoly对象不是我希望我的类的用户做的事情.myRectangle和myTriangle都派生自myPoly,而mySquare派生自myRectangle.我还包括了我计划测试课程的测试程序.我正在使用Code :: Blocks 10.05并在构建test.cpp程序时不断收到以下错误:

undefined reference to 'myPoly::myPoly()'
Run Code Online (Sandbox Code Playgroud)

我为myPoly类的方法得到了42个类似的错误.当我尝试为myRectangle和myTriangle构建.cpp文件时会发生这种情况.通过我试图对这个小项目遇到的问题进行的研究,我觉得我的包含警卫或我的#include语句出了问题,而且没有正确包含或者包含太多次.起初我将myPoly的.cpp文件提供给myRectangle和myTriangle,但是读取了几个地方,包括myPoly的.h文件更有效,还有一些如何自动包含它的.cpp.如果有人能提供一些有关的信息,我将不胜感激.我还记得一些关于如何在包含语句中使用引号与使用尖括号不同的内容.以下是我为我的小项目制作的所有九个文件.大多数评论都是对我的一些注释或提醒.

myPoly.h

//Practice with inheritance, polymorphism, and Abstract Data Types
//header file for Polygon class

#ifndef MYPOLY_H
#define MYPOLY_H

class myPoly
{
    public:
        //constructor
        //const reference pass because the values w and h don't change and reference avoid the time it takes to copy large
        //  objects by value (if there were any)
        myPoly();
        myPoly(const float & w, const float & h);

        //destructor
        virtual ~myPoly();

        //accessors
        float getWidth();
        float getHeight();
        void setWidth(const float & w);
        void setHeight(const float & h);

        virtual float area() = 0;

    private:
        float width, height;
};

#endif
Run Code Online (Sandbox Code Playgroud)

myPoly.cpp

//Practice with inheritance, polymorphism, and Abstract Data Types
//implementation file for myPoly class

#include "myPoly.h"

//constructor
myPoly::myPoly()
{
    setWidth(10);
    setHeight(10);
}

myPoly::myPoly(const float & w, const float & h)
{
    setWidth(w);
    setHeight(h);
}

//destructor
myPoly::~myPoly() {}

//accessors
float myPoly::getWidth() {return width;}
float myPoly::getHeight() {return height;}

void myPoly::setHeight(const float & w) {width = w;}
void myPoly::setWidth(const float & h) {height = h;}

//pure virtual functions have no implementation
//area() is handled in the header file
Run Code Online (Sandbox Code Playgroud)

myRectangle.h

//Practice with inheritance, polymorphism, and Abstract Data Types
//declaration file for myRectangle class

#ifndef MYRECTANGLE_H
#define MYRECTANGLE_H

#include "myPoly.h"

class myRectangle : public myPoly
{
    public:
        //constructor
        myRectangle();
        myRectangle(const float & w, const float & h);

        //destructor
        ~myRectangle();

        //this doesn't need to be virtual since the derived class doesn't override this method
        float area();
};

#endif
Run Code Online (Sandbox Code Playgroud)

myRectangle.cpp

//Practice with inheritance, polymorphism, and Abstract Data Types
//implementaion file for the myRectangle class

//get a vauge compiler/linker error if you have virtual methods that aren't implemented (even if it ends up being just
//  a 'stub' method, aka empty, like the destructor)

#include "myRectangle.h"

myRectangle::myRectangle()
{
    setWidth(10);
    setHeight(10);
}

myRectangle::myRectangle(const float & w, const float & h)
{
    setWidth(w);
    setHeight(h);
}

myRectangle::~myRectangle()
{
}

float myRectangle::area()
{
    return getWidth() * getHeight();
}
Run Code Online (Sandbox Code Playgroud)

myTriangle.h

//Practice with inheritance, polymorphism, and Abstract Data Types
//declaration file for myTriangle class

#ifndef MYTRIANGLE_H
#define MYTRIANGLE_H

#include "myPoly.h"

//imagine the triangle is a right triangle with a width and a height
//  |\
//  | \
//  |  \
//  |___\

class myTriangle : public myPoly
{
    public:
        //constructors
        myTriangle();
        myTriangle(const float & w, const float & h);

        //destructor
        ~myTriangle();

        //since nothing derives from this class it doesn't need to be virtual and in turn neither does the destructor
        float area();
};

#endif
Run Code Online (Sandbox Code Playgroud)

myTriangle.cpp

//Practice with inheritance, polymorphism, and Abstract Data Types
//implementation file for myTriangle class

#include "myTriangle.h"

myTriangle::myTriangle()
{
    setWidth(10);
    setHeight(10);
}

myTriangle::myTriangle(const float & w, const float & h)
{
    setWidth(w);
    setHeight(h);
}

myTriangle::~myTriangle()
{
}

float myTriangle::area()
{
    return getWidth() * getHeight() / 2;
}
Run Code Online (Sandbox Code Playgroud)

mySquare.h

//Practice with inheritance, polymorphism, and Abstract Data Types
//declaration file for mySquare class

#ifndef MYSQUARE_H
#define MYSQUARE_H

#include "myRectangle.cpp"

class mySquare : public myRectangle
{
    public:
        //constructors
        mySquare();
        //explicity call the myRectangle constructor within this implementation to pass w as width and height
        mySquare(const float w);

        //destructor
        ~mySquare();
};

#endif
Run Code Online (Sandbox Code Playgroud)

mySquare.cpp

//Practice with inheritance, polymorphism, and Abstract Data Types
//implementation file for mySquare class

#include "mySquare.h"

mySquare::mySquare()
{
    setWidth(10);
    setHeight(10);
}

mySquare::mySquare(const float w)
{
    myRectangle::myRectangle(w, w);
}

mySquare::~mySquare()
{
}
Run Code Online (Sandbox Code Playgroud)

TEST.CPP

//Practice with inheritance, polymorphism, and Abstract Data Types
//main class that uses my shape classes and experiments with inheritance, polymorphism, and ADTs

#include "myRectangle.cpp"
//#include "mySquare.cpp"
#include "myTriangle.cpp"

#include <iostream>

int main()
{
    myPoly * shape = new myRectangle(20,20);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我很好奇为什么我得到这些错误或为什么我做的事情可能不被认为是好的/最佳实践,而不是仅仅接收一行代码以使我的错误消失.

Cod*_*ice 6

你的包含警卫看起来很好.如果不是,您很可能会收到编译器错误,包括文件和行号信息.您发布的错误似乎更像是链接器错误.

但是,您的代码存在一个"问题".作为一般规则,您应该只有#include.h文件而不是.cpp文件.

现在来解决方案:我自己不熟悉Code :: Blocks.但是,我希望我能提供一些能够指出正确方向的一般信息.我在过去默认使用的一些编译器允许我编译单个C++文件并运行该程序.要编译包含多个文件的程序,我必须创建一个项目.(大多数现代编译器都强迫您从一开始就创建一个项目.)考虑到这一点,我建议您查看如何在Code :: Blocks中为您的程序创建项目.


Joh*_*0te 5

从代码的角度来看(至少我看过的),它看起来不错,但是:

有两件事需要考虑:

  1. 不要直接包含 cpp 文件。例如,在 mySquare.h 中,#include "myRectangle.cpp"应该是#include "myRectangle.h". 您希望包含头文件中提供的接口/声明,这些接口/声明告诉程序如何创建类,而不仅仅是函数定义。

  2. 其次,确保使用所有目标文件进行编译。我不知道代码块,但如果你使用 g++ 或类似的东西,你会想要g++ main.cpp myPoly.cpp mySquare.cpp etc.为所有文件做。例如,如果您忘记了 myPoly.cpp,可能会发生这样的错误,因为将不包含其函数的定义。