簡體   English   中英

在C ++中定義類的問題

[英]Problems Defining Class in C++

我是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中的內容,但這不能解決問題。


這是我收到的錯誤消息:

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

您正在cpp中重新聲明Sun類。 #include本質上復制並粘貼了頭文件。 因此,您要創建兩個具有相同類名的聲明,並且編譯器不知道該選擇哪個。 從cpp中刪除類聲明,它應該可以工作。

問題是您的.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;
}

暫無
暫無

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

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