0

I am new to C++ programming, but have a background in Java programming. I am trying to create a Class using .cpp and .h files so that they can be included in any project. After implementing the following code below, I am receiving multiple errors such as "use of undefined type 'SUN'" and 'zsizei: undeclared identifier'. As far as I can tell, I've exactly followed various tutorials and references in the implementation, but I am sure there is an error in my code, or I would not be having any problems.

Here is sun.h:

#ifndef SUN_H
#define SUN_H

class Sun {
public:
    void init(float xsize, float ysize, float zsize);
    void draw();
private:
    float xsizei; //size of interior
    float ysizei; //size of interior
    float zsizei; //size of interior

    float xsizee; //size of exterior
    float ysizee; //size of exterior
    float zsizee; //size of exterior

    float xySlices;
    float yzSlices;
    float thetaXY;
    float thetaYZ;

        float ratio;
    };

#endif

Here is sun.cpp:

#ifdef _APPLE_
#  include <GL/glew.h>
#  include <GL/freeglut.h>
#  include <OpenGL/glext.h>
#else
#  include <GL/glew.h>
#  include <GL/freeglut.h>
//#  include <GL/glext.h>
#pragma comment(lib, "glew32.lib") 
#endif

# include <math.h>
# include "sun.h"

# define PI 3.141569

class Sun {
public:
    void init(float xsize, float ysize, float zsize);
    void draw();
private:
    float xsizei; //size of interior
    float ysizei; //size of interior
    float zsizei; //size of interior

    float xsizee; //size of exterior
    float ysizee; //size of exterior
    float zsizee; //size of exterior

    float xySlices = 36;
    float yzSlices = 36;
    float thetaXY = xySlices / (2 * PI);
    float thetaYZ = yzSlices / (2 * PI);

    float ratio = 0.8;
};

/**
* Object will be drawn with its origin in its center
*/
void Sun::init(float xs, float ys, float zs) {
    xsizei = xs * ratio;
    ysizei = ys * ratio;
    zsizei = zs * ratio;

    xsizee = xs * (1 - ratio);
    ysizee = ys * (1 - ratio);
    zsizee = zs * (1 - ratio);
}

/*
* Draw this object
*/
void Sun::draw() {
    //first, draw the ball part
    for (int i = 0; i < xySlices; i++) {

        float yStart = ysizei / 2 * sin(thetaYZ * i);
        float yEnd = ysizei / 2 * sin(thetaYZ * (i + 1));

        float zStart = zsizei / 2 * sin(thetaYZ * i);
        float zEnd = zsizei / 2 * sin(thetaYZ * (i + 1));

        for (int j = 0; j < yzSlices; j++) {
            float xStart = xsizei / 2 * cos(thetaXY * j);
            float xEnd = xsizei / 2 * cos(thetaXY * (j + 1));

            glVertex3f(xStart, yStart, zStart);
            glVertex3f(xStart, yEnd, zEnd);
            glVertex3f(xEnd, yEnd, zEnd);

            glVertex3f(xEnd, yEnd, zEnd);
            glVertex3f(xEnd, yStart, zStart);
            glVertex3f(xStart, yStart, zStart);
        }

    }
}

I thought I might be duplicating the declarations, so I tried omitting the tings in sun.cpp which were already declared, but this did not fix the problem.


Here are the error messages I am receiving:

Severity    Code    Description Project File    Line    Suppression State
Error   C2065   'ratio': undeclared identifier  Proj    c:\\sun.cpp 48  
Error   C2027   use of undefined type 'Sun' Proj    c:\\sun.cpp 41  
Error   C2027   use of undefined type 'Sun' Proj    c:\\sun.cpp 54  
Error   C2065   'zsizei': undeclared identifier Proj    c:\\sun.cpp 44  
Error   C2065   'zsizei': undeclared identifier Proj    c:\\sun.cpp 61  
Error   C2065   'zsizei': undeclared identifier Proj    c:\\sun.cpp 62  
Error   C2065   'zsizee': undeclared identifier Proj    c:\\sun.cpp 48  
Error   C2065   'yzSlices': undeclared identifier   Proj    c:\\sun.cpp 64  
Error   C2065   'ysizei': undeclared identifier Proj    c:\\sun.cpp 43  
Error   C2065   'ysizei': undeclared identifier Proj    c:\\sun.cpp 58  
Error   C2065   'ysizei': undeclared identifier Proj    c:\\sun.cpp 59  
Error   C2065   'ysizee': undeclared identifier Proj    c:\\sun.cpp 47  
Error   C2065   'xySlices': undeclared identifier   Proj    c:\\sun.cpp 56  
Error   C2065   'xsizei': undeclared identifier Proj    c:\\sun.cpp 42  
Error   C2065   'xsizei': undeclared identifier Proj    c:\\s...
Brandon Dixon
  • 1,036
  • 9
  • 16

2 Answers2

0

You are re-declaring the Sun class in the cpp. #include essentially copies and pastes the header file. So you are creating two declarations of the same class name and the compiler does not know which one to choose. Remove the class declaration from the cpp and it should work.

Garrigan Stafford
  • 1,331
  • 9
  • 20
0

The problem is that your .cpp file is redeclaring the sun class. What instead you need to is that remove class Sun from .cpp.

To achieve .h/.cpp format you do something like this :

In .h

#ifndef SUN_H
#define SUN_H
class Sun { 
int x;
public : 
    void foo(int);
}
#endif

In .cpp

#include "sun.h"

//define the function no need to write class sun again
void foo(int s) {
x = s;
}
coder3101
  • 3,920
  • 3
  • 24
  • 28