[英](structname/classname) does not name a type C++
我的結構和類沒有“類型”有問題我的代碼看起來像這樣,我將顯示一個錯誤:
這是我在 vector.h 中的“Vector”類下聲明的結構“intersection”:
struct Intersection {
bool hasIntersection;
Shape *pShape;
float t;
Color color;
Intersection emptyIntersection();};
然后這是我在 Shape.cpp 中的包含,其中我有一個具有不同形狀的子類結構:
#include "../include/Shape.h"
這是其中一項功能:
Intersection Sphere::intersect(const Ray &ray) {
// Sphere naar nul zetten
Ray localray = ray;
localray.origin -= centre;
Intersection localintersection{};
float a = localray.direction.length2();
float b = 2 * localray.direction.dot(localray.origin);
float c = localray.origin.length2() - sqr(radius);
float D = sqr(b) - 4 * a * c;
if (D < 0.0f){
return localintersection.emptyIntersection();
}
float t1 = (-b - std::sqrt(D)) / (2 * a);
float t2 = (-b + std::sqrt(D)) / (2 * a);
if (t1 > RAY_T_MIN && t1 < RAY_T_MAX) {
localintersection.hasIntersection = true;
localintersection.pShape = this;
localintersection.color = color;
localintersection.t = t1;
}
else if(t2 > RAY_T_MIN && t2 < RAY_T_MAX) {
localintersection.t = t2;
}
else{
return localintersection.emptyIntersection();
}}
這是我在 Shape.h 中的 Shape 類的頂部部分
virtual Intersection intersect(const Ray& ray) = 0;
它給我的錯誤是“Shape”沒有為我的結構命名類型,而 Intersection 沒有為我的 Shape.h 類命名類型。 我不知道如何解決它,我看到了許多其他有解決方案的主題,但這些都不起作用
編輯:
Shape 和子類的聲明
class Shape {
public:
virtual Intersection intersect(const Ray& ray) = 0;
};
class Shapeset : public Shape{
protected:
std::vector<Shape*> shapes;
public:
Shapeset();
void addShape (Shape* shape);
virtual Intersection intersect(const Ray& ray);
};
帶有 Intersection 聲明的標題沒有 Shape 聲明。
使用代碼:
class Shape;
struct Intersection { ...
Shape header 也有同樣的問題:header 沒有 Intersection 聲明。 我建議您將交集標題包含在形狀標題中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.