Base class has incomplete type

后端 未结 1 665
谎友^
谎友^ 2021-01-16 17:12

I have a base class Point from which I inherit in Point3D. However, for some reason class Point must always return Point3D

相关标签:
1条回答
  • 2021-01-16 17:57

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

    0 讨论(0)
提交回复
热议问题