Test Test
Test Test

Reputation: 2889

'does not name a type' error in C++

I have the following header file:

#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);
};

now when I compile my code I get this:

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

and

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

What does the

‘Vertex’ does not name a type

means? And why do I get

‘class Sphere’ has no member named ‘create_sphere’

since I have this in my 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

EDIT: This is included in my test_1.cpp where the "main" method lies.

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

using namespace std;

//Vertex Structure
struct Vertex {

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

Then I create a Sphere like this

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

How I am supposed to include Vertex in the Sphere.h file?

Upvotes: 1

Views: 10639

Answers (3)

Mr Lister
Mr Lister

Reputation: 46559

What is the definition of Vertex? Maybe you need a namespace for it?
And the second error is caused by the first: since the compiler doesn't know what a Vertex* is, it can't create the create_sphere function.

Upvotes: 0

Mike Seymour
Mike Seymour

Reputation: 254461

‘Vertex’ does not name a type

This means that the compiler has not seen a declaration of Vertex, and so doesn't know that it's a type. Presumably it's defined in a header file that you're not including; you should include that from your header file.

(If it's a class type, then you only need to add a forward declaration (class Vertex;) to your Sphere.h, and include the header from Sphere.cpp. This would be a better option, since it doesn't introduce a header file dependency.)

‘class Sphere’ has no member named ‘create_sphere’

This is a result of the previous error; the compiler failed to understand the declaration of create_sphere, so does not know that it exists. Fixing the first error will also fix this.

Upvotes: 1

sje397
sje397

Reputation: 41822

It means that the compiler does not know what a Vertex is. It is not defined (or is not defined properly) in any of the header files that you included. Hence the function that tries to return a pointer to one cannot be compiled either.

Because you are only dealing with a pointer to a Vertex here in your header file, you could forward declare the class:

class Vertex;

class Sphere
{   
    public:
      // ...

...but then, you do have to include the proper definition in your cpp file before accessing any methods or other members of the class.

Upvotes: 4

Related Questions