2012-01-10 3 views
0

나는 다음과 같은 헤더 파일이 : 지금은 내 코드를 컴파일 할 때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’ 

무엇이

'꼭지점'은 유형을 지정하지 않습니다.

을 의미합니까? 그리고 왜 내가 어떻게해야합니까

내 Sphere.cpp이이 있기 때문에 '클래스 구체는'더 멤버 'create_sphere'

이름이 없다 :

//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 "main"메서드가있는 곳에 포함되어 있습니다.

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

using namespace std; 

//Vertex Structure 
struct Vertex { 

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

그때 나는이 파일 Sphere.h의 정점을 포함하는 가정하고 어떻게

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

같은 구체를 생성?

+0

'Vertex'라는 선언이 누락되었습니다. 어쩌면 헤더를'#include '하는 것을 잊었을 까? 선언이 같은 파일에 있다면, * Sphere의 정의 앞에 * 선언을 넣어야합니다. 다른 오류는 첫 번째 오류의 눈덩이 효과에 불과합니다. –

+0

헤더? 꼭지점은 구조 변수 일뿐입니다. 어떻게 포함시켜야합니까? –

+0

별도의 머리글에 없으면 '구형'정의보다 위에 있어야합니다. –

답변

4

컴파일러는 Vertex이 무엇인지 알지 못합니다. 포함 된 헤더 파일에서 정의되지 않았거나 제대로 정의되지 않았습니다. 따라서 하나의 포인터를 반환하려는 함수는 컴파일 할 수 없습니다. 당신은 단지 여기 헤더 파일에 Vertex에 대한 포인터를 처리하기 때문에

, 당신은 클래스 선언 전달할 수 :

class Vertex; 

class Sphere 
{ 
    public: 
     // ... 

을 ...하지만 그런 다음에 적절한 정의를 포함해야합니까 cpp 파일을 사용하여 클래스의 메소드 또는 다른 멤버에 액세스하십시오.

+0

내 꼭지점은 Sphere.cpp의 구조체입니다. –

+0

편집 : 확인 했으므로 test_1.cpp와 Sphere.cpp에서 Vertex를 선언하고 test_1.cpp와 Sphere.cpp에 Sphere.h를 포함해야했습니다. –

+1

@TestTest : 이 작업을 수행하는 적절한 방법은 Vertex 구조체를 자체 .h 파일에 배치하고 첨부 된 .cpp의 Vertex에서 모든 작업을 구현 한 다음 해당 포함 파일을 추가하는 것입니다. 좋은 규칙은 "클래스 또는 구조 당 한 쌍의 헤더 - 소스"입니다. 예외는 오직 파일의 메인 클래스에서만 사용되는 헬퍼 클래스 또는 유형입니다. – datenwolf

0

Vertex의 정의는 무엇입니까? 어쩌면 네임 스페이스가 필요할까요?
두 번째 오류는 첫 번째 오류로 인해 발생합니다. 컴파일러는 Vertex *가 무엇인지 모르기 때문에 create_sphere 함수를 만들 수 없습니다.

1

'정점'이 컴파일러는 Vertex의 선언을 보지 않았 음을 의미 유형

의 이름이없는, 그래서 그것은 종류인지 알 수 없습니다. 아마도 헤더 파일에 정의되어 있지 않습니다. 헤더 파일의 내용을 포함시켜야합니다.이 클래스 유형이 있다면

은 (, 당신은 당신 Sphere.h에 앞으로 선언 (class Vertex;)를 추가해야하고, Sphere.cpp에서 헤더를 포함합니다. 이것은 더 나은 옵션이 될 것입니다, 그것은 헤더 파일을 소개하지 않기 때문에 . 의존성)

'클래스 구체'이 이전 에러의 결과이다

'create_sphere'라는 이름의 부재가 없다; 컴파일러가 create_sphere의 선언을 이해하지 못 했으므로 컴파일러가 존재한다는 것을 알지 못합니다. 첫 번째 오류를 수정하면이 문제도 해결됩니다.