簡體   English   中英

C ++中的“未命名類型”錯誤

[英]'does not name a type' error in 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 <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);
};

現在,當我編譯代碼時,我得到以下信息:

Sphere.h:18:3: error: ‘Vertex’ does not name a type
Sphere.cpp:48:1: error: ‘Vertex’ does not name a type

test_1.cpp: In function ‘int main()’: 
test_1.cpp:318:38: error: ‘class Sphere’ has no member named ‘create_sphere’

那是什么

“頂點”未命名類型

手段? 為什么我得到

'class Sphere'沒有名為'create_sphere'的成員

因為我在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;
}

Thansk

編輯:這包含在我的test_1.cpp中,其中的“主要”方法位於其中。

#include "Sphere.h"
#include "Terrain.h"

using namespace std;

//Vertex Structure
struct Vertex {

    GLdouble position[3];
    GLfloat color[3];
    GLfloat texture[2];
};

然后我創建一個這樣的球體

 sphere_vertices_final = planet_1->create_sphere(5,5,no_sphere_vertices);

我應該如何在Sphere.h文件中包含Vertex?

這意味着編譯器不知道什么是Vertex 您包含的任何頭文件中都沒有定義(或定義不正確)。 因此,試圖返回一個指針的函數也無法編譯。

因為您只在頭文件中處理指向Vertex的指針,所以可以向前聲明該類:

class Vertex;

class Sphere
{   
    public:
      // ...

...但是然后,在訪問任何方法或類的其他成員之前,您必須在cpp文件中包含正確的定義。

“頂點”未命名類型

這意味着編譯器沒有看到Vertex的聲明,因此也不知道它是類型。 大概是在不包含的頭文件中定義的; 您應該從頭文件中添加它。

(如果是類類型,則只需向Sphere.h添加前向聲明( class Vertex; ),並包含Sphere.cpp的標頭。這將是一個更好的選擇,因為它不會引入頭文件依賴性。)

'class Sphere'沒有名為'create_sphere'的成員

這是先前錯誤的結果; 編譯器無法理解create_sphere的聲明,因此不知道它的存在。 修正第一個錯誤也將解決此問題。

Vertex的解釋是什么? 也許您需要一個名稱空間?
第二個錯誤是由第一個錯誤引起的:由於編譯器不知道什么是Vertex *,因此無法創建create_sphere函數。

暫無
暫無

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

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