2014-01-09 90 views
0

这里的point.h未知类型名称矢量

#ifndef POINT_H_ 
#define POINT_H_ 
#include "/Users/Verdonckt/Documents/3dlaboNogMaarEens/3dlaboNogMaarEens/src/util/Vector.h" 
class Point { 

public: 

    double x; 
    double y; 
    double z; 

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

    void set(double x, double y, double z); 
    friend Vector operator-(const Point& left, const Point& right); 
}; 

#endif /* POINT_H_ */ 

此文件提供了有关行错误:

friend Vector operator-(const Point& left, const Point& right); 

说未知类型名称: 我认为,包括作品,因为它不给如果将其更改为不存在的文件,则会发生错误。

这里的Vector.h:再次

#ifndef VECTOR_H_ 
#define VECTOR_H_ 

#include "Point.h" 
class Vector { 

public: 

double x, y, z; 

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

Vector(const Point & from, const Point & to); 

Vector(const Point& p):x(p.x),y(p.y), z(p.z){ } 
    double getX(){return x;} 
    double getY(){return y;} 
    double getZ(){return z;} 

    friend Vector operator*(const Vector & v, double a); 

    friend Vector operator*(double a, const Vector & v); 

    friend Point operator+(const Point & p, const Vector & v); 

    friend Point operator+(const Vector & v, const Point & p); 

    friend Vector operator+(const Vector & v1, const Vector & v2); 
    friend Vector operator-(const Vector& v1, const Vector& v2); 

    double dot(const Vector & v); 

    Vector cross(const Vector & v); 
    double length(); 
    void normalize(); 

}; 

#endif /* VECTOR_H_ */ 

一旦说上线:

friend Point operator+(const Point & p, const Vector & v); 
friend Point operator+(const Vector & v, const Point & p); 
Vector(const Point & from, const Point & to); 
Vector(const Point& p):x(p.x),y(p.y), z(p.z){ } 

未知类型名称 '点'

这是为什么?如何解决这个问题?

+4

你有一个标题之间的循环依赖关系。 –

+0

可能的重复[错误2061 - 类成为“未定义”,当我包括一个标题](http://stackoverflow.com/questions/8010601/error-2061-class-becomes-undefined-when-i-include-a-标题) –

回答

4

你有一个循环依赖 - Vector.hPoint.h每个尝试包括其他。包括警卫将防止严重的问题,但最终会有一个在另一个之前定义的类,而第二个的名称在第一个中不可用。

幸运的是,Point不需要Vector的完整定义 - 前向声明将执行。

class Vector; 
class Point { 
    // ... 

    // No problem using `Vector` to declare a return type here 
    friend Vector operator-(const Point& left, const Point& right); 
}; 
+0

嗨!这个作品我放弃了包含在类中点添加类Vector你告诉我。非常感谢! –