简体   繁体   English

未知类型名称向量

[英]Unknown type name Vector

Here's point.h 这是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_ */

This files gives an error on line: 该文件在网上出现错误:

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

Saying unknown type name: I think the include works since it doesn't give an error an it would if I change it to a non existing file. 说未知的类型名称:我认为include可以正常工作,因为它不会产生错误,如果将其更改为不存在的文件也可以。

here's Vector.h: 这是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_ */

Once again saying on lines: 在网上再次说:

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){ }

Unknown type name 'Point' 未知类型名称“ Point”

Why is this and how do I resolve this? 为什么会这样,我该如何解决?

You have a circular dependency - Vector.h and Point.h each try to include the other. 您有一个循环依赖项Vector.hPoint.h各自尝试包含另一个。 The include guards will prevent serious problems, but you'll end up with one of the classes defined before the other, and the name of the second will not be available within the first. 包含防护将防止出现严重的问题,但是您将最终在其中一个类别之前定义一个类别,而第二个类别的名称将在第一个类别中不可用。

Luckily, Point doesn't need a full definition of Vector - a forward declaration will do. 幸运的是, 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);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM