在C ++中定义类的问题

时间:2018-04-17 16:18:54

标签: c++ class instantiation

我是C ++编程的新手,但有Java编程背景。我正在尝试使用.cpp和.h文件创建一个类,以便它们可以包含在任何项目中。在实现下面的代码之后,我收到了多个错误,例如"使用未定义的类型' SUN'"和' zsizei:未声明的标识符'。据我所知,我已经完全遵循了实现中的各种教程和参考,但我确信我的代码中存在错误,或者我不会遇到任何问题。

这是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

这是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);
        }

    }
}

我以为我可能会复制声明,所以我尝试省略已经声明的sun.cpp中的tings,但这并没有解决问题。

以下是我收到的错误消息:

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...

2 个答案:

答案 0 :(得分:0)

您正在重新声明cpp中的Sun类。 #include基本上复制并粘贴头文件。所以你要创建两个相同类名的声明,编译器不知道选择哪一个。从cpp中删除类声明,它应该可以工作。

答案 1 :(得分:0)

问题是您的.cpp文件正在重新声明sun类。你需要的是从.cpp中删除类Sun。

要实现.h / .cpp格式,您可以执行以下操作:

在.h

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

在.cpp

#include "sun.h"

//define the function no need to write class sun again
void foo(int s) {
x = s;
}