另一个不完整的类型错误C

another incomplete type Error C++

本文关键字:类型 错误 另一个      更新时间:2023-10-16

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

编写一个litte c 程序并感到困惑....

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

在第13行和" Expect';'"中获取错误消息" field'Dot'具有不完整的类型"。befor'(" token"同一行

错误似乎从vector3.CPP开始,其中仅包括vector3.h和空方法

删除了正常3个标头中的包含" vector3.h",思想将在一个圆圈中运行...

一些想法?希望如此:)和ty for Answers

这是我的两个重要课程:

#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

aaaaaaaaaaaaaaaaaaaaaaaaand

#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

它只是意味着编译器当时不知道Vector3是什么。如果您提前声明,错误将消失:

#ifndef NORMAL3_H
#define NORMAL3_H
class Vector3;  // Add this line
class Normal3 {
  // ...
};

更新:正如约翰在评论中所说的那样,在vector3.h中替换 #include "Normal3.h"的良好进步将是另一个远期声明, class Normal3;

#ifndef VECTOR3_H
#define VECTOR3_H
class Normal3; // instead of #include "Normal3.h"
class Vector3 {
  // ...
};

您应该尝试将#include指令在标题中保持在最低限度,以避免过度汇编依赖性。您只需要在定义您正在使用的类型时包含标头(通常是因为您定义的类具有此类型的数据成员)。如果您仅使用指示或引用该类型的该类型或功能参数(如示例中),则向前声明就足够了。

您的文件normal3.hVector3一无所知。

我看到 Vector3 v不会更改为 dot,然后您应该写:

而不是Normal3 dot(Vector3 v); //Line 13

AS

#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