C++ ';不命名类型';C+中的错误+;
我有以下头文件:C++ ';不命名类型';C+中的错误+;,c++,C++,我有以下头文件: #include <stdlib.h> #include <stdio.h> #include <string.h> #include <stddef.h> #include <GL/glew.h> #include <GL/glfw.h> #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <GL/glew.h>
#include <GL/glfw.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <stdlib.h>
#include <vector>
class Sphere
{
public:
Sphere();
int count_sphere_vertices(int ,int, int);
Vertex* create_sphere(int ,int , int);
};
及
这是什么意思
“Vertex”未命名类型
意味着什么?那我为什么会
“类球体”没有名为“创建球体”的成员
因为我的Sphere.cpp中有这个:
//Calculating points for the sphere (the algorithm is implemented here)
Vertex* Sphere::create_sphere(int dtheta,int dphi, int no_vertices)
{
GLdouble x,y,z,x2,y2,z2;
GLdouble magnitude=0;
int n=-1;
int theta,phi;
const double PI = 3.1415926535897;
GLdouble DTOR = (PI/180);//degrees to radians
Vertex* sphere_vertices = new Vertex[no_vertices];
for (theta=-90;theta<=90-dtheta;theta+=dtheta) {
for (phi=0;phi<=360-dphi;phi+=dphi) {
//calculating Vertex 1
x = cos(theta*DTOR) * cos(phi*DTOR);
y = cos(theta*DTOR) * sin(phi*DTOR);
z = sin(theta*DTOR);
n+=1;
sphere_vertices[n].position[0] = x;
sphere_vertices[n].position[1] = y;
sphere_vertices[n].position[2] = z;
//calculating Vertex 2
x = cos((theta+dtheta)*DTOR) * cos(phi*DTOR);
y = cos((theta+dtheta)*DTOR) * sin(phi*DTOR);
z = sin((theta+dtheta)*DTOR);
n+=1;
sphere_vertices[n].position[0] = x;
sphere_vertices[n].position[1] = y;
sphere_vertices[n].position[2] = z;
//calculating Vertex 3
x = cos((theta+dtheta)*DTOR) * cos((phi+dphi)*DTOR);
y = cos((theta+dtheta)*DTOR) * sin((phi+dphi)*DTOR);
z = sin((theta+dtheta)*DTOR);
n+=1;
sphere_vertices[n].position[0] = x;
sphere_vertices[n].position[1] = y;
sphere_vertices[n].position[2] = z;
//adding Vertex_1 again to divide the Quad into 2 triangles so it can be later filled with triangles!!!
//adding Vertex 1 again!
x = cos(theta*DTOR) * cos(phi*DTOR);
y = cos(theta*DTOR) * sin(phi*DTOR);
z = sin(theta*DTOR);
n+=1;
sphere_vertices[n].position[0] = x;
sphere_vertices[n].position[1] = y;
sphere_vertices[n].position[2] = z;
if (theta > -90 && theta < 90) {
//calculating Vertex 4
x = cos(theta*DTOR) * cos((phi+dphi)*DTOR);
y = cos(theta*DTOR) * sin((phi+dphi)*DTOR);
z = sin(theta*DTOR);
n+=1;
sphere_vertices[n].position[0] = x;
sphere_vertices[n].position[1] = y;
sphere_vertices[n].position[2] = z;
}
}
}
//Setting the color
for(int i=0; i<no_vertices; i+=1)
{
sphere_vertices[i].color[0] = 1;
sphere_vertices[i].color[1] = 0;
sphere_vertices[i].color[2] = 0;
}
printf("%d >> \n", n);
return sphere_vertices;
}
然后我创建一个像这样的球体
sphere_vertices_final = planet_1->create_sphere(5,5,no_sphere_vertices);
如何在Sphere.h文件中包含顶点?这意味着编译器不知道什么是
顶点。它未在包含的任何头文件中定义(或未正确定义)。因此,尝试返回指向指针的函数也无法编译
因为您只处理头文件中指向顶点的指针,所以可以向前声明类:
class Vertex;
class Sphere
{
public:
// ...
…但是,在访问任何方法或类的其他成员之前,您必须在cpp文件中包含正确的定义。这意味着编译器不知道什么是顶点。它未在包含的任何头文件中定义(或未正确定义)。因此,尝试返回指向指针的函数也无法编译
因为您只处理头文件中指向顶点的指针,所以可以向前声明类:
class Vertex;
class Sphere
{
public:
// ...
…但是,在访问任何方法或类的其他成员之前,必须在cpp文件中包含正确的定义。顶点的定义是什么?也许你需要一个名称空间
第二个错误是由第一个错误引起的:因为编译器不知道顶点*是什么,所以无法创建create_sphere函数。顶点的定义是什么?也许你需要一个名称空间
第二个错误是由第一个错误引起的:因为编译器不知道顶点*是什么,所以不能创建create_sphere函数
“Vertex”未命名类型
这意味着编译器没有看到顶点的声明,因此不知道它是一个类型。它大概是在一个头文件中定义的,您不包括它;你应该从你的头文件中包含它
(如果是类类型,则只需向Sphere.h
中添加一个正向声明(class Vertex;
),并包含Sphere.cpp
中的头文件。这是一个更好的选择,因为它不引入头文件依赖关系。)
“类球体”没有名为“创建球体”的成员
这是先前错误的结果;编译器无法理解create\u sphere
的声明,因此不知道它存在。修复第一个错误也将修复此问题
“Vertex”未命名类型
这意味着编译器没有看到顶点的声明,因此不知道它是一个类型。它大概是在一个头文件中定义的,您不包括它;你应该从你的头文件中包含它
(如果是类类型,则只需向Sphere.h
中添加一个正向声明(class Vertex;
),并包含Sphere.cpp
中的头文件。这是一个更好的选择,因为它不引入头文件依赖关系。)
“类球体”没有名为“创建球体”的成员
这是先前错误的结果;编译器无法理解create\u sphere
的声明,因此不知道它存在。修复第一个错误也将修复此问题。您缺少顶点的声明。也许您忘记了将其标题包含在内?如果声明位于同一文件中,则需要将其放置在Sphere
的定义之前。另一个错误只是第一个错误的雪球效应。它的标题是什么?顶点只是一个结构变量。我应该如何包含它?如果它不在单独的标题中,它需要在球体
的定义之上。如果Vertex是一个变量,它不是一个类型,是吗。这就是编译器试图告诉您的。您缺少Vertex
的声明。也许您忘记了将其标题包含在内?如果声明位于同一文件中,则需要将其放置在Sphere
的定义之前。另一个错误只是第一个错误的雪球效应。它的标题是什么?顶点只是一个结构变量。我应该如何包含它?如果它不在单独的标题中,它需要在球体
的定义之上。如果Vertex是一个变量,它不是一个类型,是吗。这就是编译器试图告诉您的。我的顶点是Sphere.cppEDIT中的一个结构:好的,找到了它,我还必须在test_1.cpp和Sphere.cpp中声明顶点,并在test_1.cpp和Sphere中包含Sphere.h。cpp@TestTest:正确的方法是将Vertex结构放入自己的.h文件中,在附带的.cpp中对顶点执行所有操作,然后添加该包含文件。一个很好的规则是“每个类或结构一对头源”-唯一的例外是仅与文件的主类一起使用的辅助类或类型。我的顶点是Sphere.cppEDIT中的一个结构:确定找到了它,我还必须在test_1.cpp和Sphere.cpp中声明顶点,并将Sphere.h包含在测试_1.cpp和Sphere中。cpp@TestTest:正确的方法是将Vertex结构放入自己的.h文件中,在附带的.cpp中对顶点执行所有操作,然后添加该include文件。一个好的规则是“每个类或结构一对头源”-唯一的例外是仅与文件的主类一起使用的帮助器类或类型。
sphere_vertices_final = planet_1->create_sphere(5,5,no_sphere_vertices);
class Vertex;
class Sphere
{
public:
// ...