C++ 错误:字段类型不完整
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3999400/
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
Error: Field has an incomplete type
提问by Aero
quaternion.h:15: error: field ‘v' has incomplete type
quaternion.h:15: 错误:字段“v”的类型不完整
Hi! I am stuck on an error that I cannot seem to solve.
你好!我陷入了一个我似乎无法解决的错误。
Below is my code:
下面是我的代码:
#ifndef QUATERNION_H
#define QUATERNION_H
#include "vec3.h"
class Vec3;
class Quaternion
{
public:
Quaternion(Vec3 v);
Quaternion(double w, Vec3 v);
Vec3 v; <--------------------------This is where the error is :(
double scalar;
Quaternion operator *(Quaternion s);
Quaternion conjugate();
};
#endif
My Vec.h looks like this:
我的 Vec.h 看起来像这样:
#ifndef VEC3_H
#define VEC3_H
#include "point.h"
#include "quaternion.h"
#include <math.h>
class Quaternion;
class Vec3
{
friend ofstream& operator <<(ofstream& output, const Vec3& p);
friend ifstream& operator >>(ifstream& input, Vec3& p);
public:
Vec3();
Vec3(double _x, double _y);
Vec3(double _x, double _y, double _z);
double x,y,z;
//Operators
Vec3 operator -(Vec3 a) const;
Vec3 operator /(double s) const;
Vec3 operator *(double s) const;
Vec3 operator *(Quaternion q) const;
// Used to do vector Vec3 addition
Vec3 operator +(Vec3 a) const;
Point operator +(Point a) const;
Vec3& operator =(Point a);
Vec3 crossProduct(Vec3 v1); // Itself cross v1
double dotProduct(Vec3 v);
double length();
void normalize();
};
#endif
Thanks for the help again =)
再次感谢您的帮助 =)
回答by AnT
Well, you have circular inclusion of two header files: vec3.h
and quaternion.h
. Include guards will make sure that each header is included only once. One of them will be included first, the other - second. In your case quaternion.h
is included first, meaning that Vec3
becomes an incomplete type in it. This is what the compiler is telling you.
好吧,您循环包含了两个头文件:vec3.h
和quaternion.h
. 包含守卫将确保每个标题只包含一次。其中一个将首先包含在内,另一个将包含在内。在您的情况下,quaternion.h
首先包括在内,这意味着它Vec3
成为其中的不完整类型。这就是编译器告诉你的。
Since you are trying to use Vec3
object as an immediate member of Quaternion
object, you absolutely need Vec3
to be a complete type. The quaternion.h
header must include vec3.h
header. The
由于您试图将Vec3
object 用作 object 的直接成员Quaternion
,因此您绝对需要Vec3
是一个完整的类型。该quaternion.h
标题必须包含vec3.h
头。这
class Vec3;
declaration achieves absolutely nothing in quaternion.h
, so you can just remove it.
声明在 中绝对没有实现quaternion.h
,因此您可以将其删除。
Given the above, it follows that vec3.h
cannotinclude quaternion.h
, or you'll end up with circular inclusion, which never achieves anything. Remove the inclusion of quaternion.h
from vec3.h
. Keep the
鉴于上述情况,因此vec3.h
不能包含quaternion.h
,否则您最终会得到循环包含,这永远不会实现任何目标。删除包含quaternion.h
from vec3.h
。保持
class Quaternion;
declaration in vec3.h
and see if it works that way.
声明vec3.h
并查看它是否以这种方式工作。
回答by Diego Sevilla
The problem is mutual inclusion of the .h
. The compiler knows the types, but at some point they are incomplete. My advice would be to just forward declare Quarternion
in the file vec3.h
, but NOT include quaternion.h
.
问题是.h
. 编译器知道类型,但在某些时候它们是不完整的。我的建议是只Quarternion
在文件中转发声明vec3.h
,但不要包含quaternion.h
.
Then, quaternion.h
can include vec3.h
and everything will compile. Also, as JaredPar suggested, remove the forward declaration of Vec3
in quaternion.h
.
然后,quaternion.h
可以包含vec3.h
,一切都会编译。此外,正如 JaredPar 所建议的,删除Vec3
in的前向声明quaternion.h
。
回答by flownt
the problem is that your files are include'ing eachother where there's no circular dependency. vec3 should not include quaternion then everything 'll be alright.
问题是您的文件在没有循环依赖的情况下包含了彼此。vec3 不应该包含四元数,然后一切都会好起来的。
now the error the compiler gave is because you predeclared vec3 but the full definition is not read if vec3 is included:
现在编译器给出的错误是因为您预先声明了 vec3 但如果包含 vec3 则不会读取完整定义:
vecr3.h --> include quaternion
quaternion.h --> include vec3 , but include-guards so nothing happens
quaternion.h --> predeclare Vec3,
quaternion.h --> try to use vec3 //fail
vec3.h --> actually declare vec3
vecr3.h --> 包含四
元数quaternion.h --> 包含 vec3 ,但包含保护,所以什么都不会发生
quaternion.h --> predeclare Vec3,
quaternion.h --> 尝试使用 vec3 //fail
vec3.h - -> 实际上声明 vec3
the only correct order is first vec3 include and only THEN the quaternion include.
唯一正确的顺序是第一个 vec3 包含,然后只有四元数包含。