簡體   English   中英

未知類型名稱向量

[英]Unknown type name Vector

這是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);

說未知的類型名稱:我認為include可以正常工作,因為它不會產生錯誤,如果將其更改為不存在的文件也可以。

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

未知類型名稱“ Point”

為什么會這樣,我該如何解決?

您有一個循環依賴項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);
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM