基类的类型不完整 [英] Base class has incomplete type
问题描述
我有一个基类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.hpp
和Point3D.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屋!