在C+中定义类的问题+; 我对C++编程很陌生,但有java编程的背景。我正在尝试使用.cpp和.h文件创建一个类,以便它们可以包含在任何项目中。在实现下面的代码之后,我收到了多个错误,例如“使用未定义的类型‘SUN’”和“zsizei:undeclared identifier”。据我所知,我在实现中完全遵循了各种教程和参考资料,但我确信我的代码中存在错误,否则我不会有任何问题
这是孙先生在C+中定义类的问题+; 我对C++编程很陌生,但有java编程的背景。我正在尝试使用.cpp和.h文件创建一个类,以便它们可以包含在任何项目中。在实现下面的代码之后,我收到了多个错误,例如“使用未定义的类型‘SUN’”和“zsizei:undeclared identifier”。据我所知,我在实现中完全遵循了各种教程和参考资料,但我确信我的代码中存在错误,否则我不会有任何问题,c++,class,instantiation,C++,Class,Instantiation,这是孙先生 #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
#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);
}
}
}
您正在cpp中声明Sun类#包括复制和粘贴头文件。因此,您正在创建两个具有相同类名的声明,而编译器不知道选择哪一个。从cpp中删除类声明,它应该可以工作。问题是.cpp文件正在重新声明sun类。相反,您需要从.cpp中删除类Sun 要实现.h/.cpp格式,请执行以下操作: In.h In.cpp
#include "sun.h"
//define the function no need to write class sun again
void foo(int s) {
x = s;
}
C++就像一支枪——你不会通过尝试和错误来学习它。阅读这本好书:请显示实际的错误信息。我假设你在某个地方有一个main。请显示codeAfter
#include“sun.h”
您在.cpp文件中已经有了类声明。你不应该重复。与java不同,C++中通常将声明和函数定义分隔成两个单独的文件。不同的语言!您不需要复制类Sun{…}
在sun.cpp中,include“sun.h”就是这样做的
#ifndef SUN_H
#define SUN_H
class Sun {
int x;
public :
void foo(int);
}
#endif
#include "sun.h"
//define the function no need to write class sun again
void foo(int s) {
x = s;
}