基类的类型不完整 [英] Base class has incomplete type

查看:157
本文介绍了基类的类型不完整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类Point,在Point3D中继承自该基类.但是,由于某些原因,类Point必须始终为操作add返回Point3D,因此我将其包含在包含中.

这是我的课程Point:

#ifndef POINT_H
#define POINT_H

#include "Point3D.hpp"

class Point{

  public:
    Point(double, double, double);

    void print() const;
    Point3D add( const Point& );

  protected:
    double mX;
    double mY;
    double mZ;

};

#endif

在我的班级Point3D中,我知道我第一次被调用时还不会遇到Point的定义(因为Point3D包含在Point标头中),所以我定义了,然后定义要使用的Point部分:

#ifndef POINT3D_H
#define POINT3D_H

#include <iostream>
#include "Point.hpp"  // leads to the same error if ommitted

class Point;    

class Point3D : public Point {

  public:
        Point3D(double, double, double);
        void print() const ;
        Point3D add(const Point&);
};

#endif

但是,这不起作用.当我编译它时,它给了我以下错误:

./tmp/Point3D.hpp:9:24: error: base class has incomplete type
class Point3D : public Point {
                ~~~~~~~^~~~~
./tmp/Point3D.hpp:7:7: note: forward declaration of 'Point'
class Point;
      ^
1 error generated.

问题此处表示要从中删除包含#include "Point.hpp"我的Point3D声明.但是,这样做会导致相同的结果,而且我认为标头后卫基本上可以完成相同的事情.

我正在使用clang进行编译.

解决方案

您不能从不完整的类型继承.您需要按以下方式构建代码:

class Point3D;

class Point
{
    // ...
    Point3D add(const Point &);
    // ...
};

class Point3D: public Point
{
    // ...
};

Point3D Point::add(const Point &)
{
    // implementation
}

函数返回类型可能不完整,这就是为什么Point的类定义如此工作的原因.

我相信您可以弄清楚如何在头文件和源文件之间进行拆分. (例如,前两部分可以放在Point.hpp中,第三部分可以在Point3D.hpp中包括Point.hpp,最后实现可以在Point.cpp中包括Point.hppPoint3D.hpp.)

I have a base class Point from which I inherit in Point3D. However, for some reason class Point must always return Point3D for the operation add, so I include it in my includes.

This is my class Point:

#ifndef POINT_H
#define POINT_H

#include "Point3D.hpp"

class Point{

  public:
    Point(double, double, double);

    void print() const;
    Point3D add( const Point& );

  protected:
    double mX;
    double mY;
    double mZ;

};

#endif

In my class Point3D, I know I will not yet have encountered the definition of Point when I will first be called (because Point3D is included in the Point header), so I define class Point;, then I define the parts I will use of Point:

#ifndef POINT3D_H
#define POINT3D_H

#include <iostream>
#include "Point.hpp"  // leads to the same error if ommitted

class Point;    

class Point3D : public Point {

  public:
        Point3D(double, double, double);
        void print() const ;
        Point3D add(const Point&);
};

#endif

However, this is not working. When I compile it, it gives me the following error:

./tmp/Point3D.hpp:9:24: error: base class has incomplete type
class Point3D : public Point {
                ~~~~~~~^~~~~
./tmp/Point3D.hpp:7:7: note: forward declaration of 'Point'
class Point;
      ^
1 error generated.

The question here would say to remove the include #include "Point.hpp" from my Point3D declaration. However, doing so leads to the same result, and I think the header-guards would basically accomplish the same thing.

I'm compiling with clang.

解决方案

You can't inherit from an incomplete type. You need to structure your code as follows:

class Point3D;

class Point
{
    // ...
    Point3D add(const Point &);
    // ...
};

class Point3D: public Point
{
    // ...
};

Point3D Point::add(const Point &)
{
    // implementation
}

Function return types may be incomplete, which is why your class definition of Point works like this.

I trust you can figure out how to split that across header and source files. (For example, the first two parts can go in Point.hpp, the third in Point3D.hpp which includes Point.hpp, and the final implementation can go in Point.cpp which includes Point.hpp and Point3D.hpp.)

这篇关于基类的类型不完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆