1

可能重复:
循环依赖/不完整类型

编写一个小 C++ 程序并感到困惑....

有 4 个类,2 个对这个错误很重要......

在第 13 行收到错误消息“字段 'dot' 的类型不完整”和“预期 ';' 在同一行中的“(”令牌”之前

错误似乎始于 Vector3.cpp,其中仅包含 Vector3.h 和空方法

删除了Normal3 Header中的include“Vector3.h”,以为会绕圈子...不太好...

一些想法?希望如此 :) 和 ty 寻求答案

这是我的两个重要课程:

#ifndef NORMAL3_H
#define NORMAL3_H


class Normal3 {


      public:

      double x, y, z;
      Normal3 mul(Normal3 n);
      Normal3 add(Normal3 n);
      Normal3 dot(Vector3 v); //Line 13

      Normal3(double x = 0, double y = 0, double z = 0)
                     : x(x), y(y), z(z)
      { }   

};

#endif //NORMAL3_H

啊啊啊啊啊啊啊啊啊啊啊

#ifndef VECTOR3_H
#define VECTOR3_H

#include "Normal3.h"

class Vector3 {

      public:

      double x, y, z, magnitude;

      Vector3 add(Vector3 v);
      Vector3 add(Normal3 n);
      Vector3 sub(Normal3 n);
      Vector3 mul(double c);
      double dot(Vector3 c);
      double dot(Normal3 n);
      Vector3 normalized();
      Normal3 asNormal();
      Vector3 refelctedOn(Normal3 n);

      Vector3(double x = 0, double y = 0, double z = 0, double m = 0)
                     : x(x), y(y), z(z), magnitude(m)
      { }

};

#endif //VECTOR3_H
4

2 回答 2

3

这只是意味着编译器不知道Vector3此时是什么。如果提前声明,错误就会消失:

#ifndef NORMAL3_H
#define NORMAL3_H

class Vector3;  // Add this line

class Normal3 {
  // ...
};

更新:正如约翰#include "Normal3.h"在他的评论中所说,在 Vector3.h 中用另一个前向声明替换将是一个很好的改进, class Normal3;

#ifndef VECTOR3_H
#define VECTOR3_H

class Normal3; // instead of #include "Normal3.h"

class Vector3 {
  // ...
};

你应该尽量减少#include头文件中的指令,以避免过度的编译依赖。如果它定义了您正在使用的类型,您只需要包含一个标题(通常是因为您正在定义的类具有此类型的数据成员)。如果您仅使用指向该类型或该类型的函数参数的指针或引用(如您的示例中所示),则前向声明就足够了。

于 2012-10-23T10:26:45.677 回答
0

你的文件normal3.h一无所知Vector3

我看到它Vector3 v不会变成dot,那么你应该写:

反而Normal3 dot(Vector3 v); //Line 13

作为

#ifndef NORMAL3_H
#define NORMAL3_H

class Vector3;

class Normal3 {


      public:

      double x, y, z;
      Normal3 mul(Normal3 n);
      Normal3 add(Normal3 n);
      Normal3 dot(const Vector3 &v); //Line 13

      Normal3(double x = 0, double y = 0, double z = 0)
                     : x(x), y(y), z(z)
      { }   

};

#endif //NORMAL3_H
于 2012-10-23T10:43:36.540 回答