gpt4 book ai didi

即使使用 ifndef,C++ 重定义错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:43:09 24 4
gpt4 key购买 nike

我有几个文件,我的错误很奇怪。我不知道这是否是问题的一部分,我使用 OpenGL/SDL 和 XCode,并使用 CMake 创建文件。

我有文件 Wall.hpp、Wall.cpp、Player.hpp、Player.cpp、controls.hpp、controls.cpp、main.cpp。

这里是文件包含的总结:

//播放器.hpp

#include <iostream>


class Player{
public :
Player();
int getLifePoints();
int getStaminaPoints();
float getPosX();
float getPosY();
float getPosZ();

void setPosX(float x);
void setPosY(float y);
void setPosZ(float z);

void reducePoints(int damage);
bool isHeroRunning();
void changeHeroRun();
bool isHeroDucking();
void changeHeroDuck();


private :
int lifePoints;
int staminaPoints;
float posX;
float posY;
float posZ;
bool isRunning;
bool isDucking;

};

//播放器.cpp

#include "Player.hpp"
using namespace std;

Player::Player(){
this->lifePoints = 100;
this->posX = 0;
this->posY = 0;
this->posZ = 0;
}

int Player::getLifePoints(){
return this->lifePoints;
}
int Player::getStaminaPoints(){
return this->staminaPoints;
}

float Player::getPosX(){
return this->posX;
};
float Player::getPosY(){
return this->posY;
};
float Player::getPosZ(){
return this->posZ;
};

void Player::setPosX(float x){
this->posX=x;
};
void Player::setPosZ(float z){
this->posZ=z;
};
void Player::setPosY(float y){
this->posY=y;
};

void Player::reducePoints(int damage){
this->lifePoints= lifePoints - damage;
}

int lifePoints;
float posX;
float posY;
float posZ;

bool Player::isHeroRunning(){
return isRunning;
}

void Player::changeHeroRun(){
this->isRunning=!this->isRunning;
}

bool Player::isHeroDucking(){
return this->isDucking;
}
void Player::changeHeroDuck(){
this->isDucking=!this->isDucking;
if (isDucking){
this->posZ=this->posZ/2;
} else {
this->posZ=this->posZ*2;
}
}

//Wall.hpp

#ifndef _WALL_H
#define _WALL_H

#include <iostream>
#include <vector>
// Include GLEW
#include <GL/glew.h>

// Include GLFW
#include <GL/glfw.h>

// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

class Wall{
public :
Wall(GLfloat x1, GLfloat x2, GLfloat z1, GLfloat z2);
//~Wall();
GLfloat getX1();
GLfloat getX2();
GLfloat getY1();
GLfloat getY2();
GLfloat getZ1();
GLfloat getZ2();
int isInZone(GLfloat x, GLfloat y, GLfloat z);
std::vector<GLfloat> add_wall (std::vector<GLfloat> walls);


private:
GLfloat x1, x2, y1, y2, z1, z2;

};

#endif

//墙.cpp

#ifndef _WALL_C
#define _WALL_C

// Include GLEW
#include <GL/glew.h>

// Include GLFW
#include <GL/glfw.h>

// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "Wall.hpp"
#include <vector>

Wall::Wall(GLfloat x1, GLfloat x2, GLfloat z1, GLfloat z2){
this->x1=x1;
this->x2=x2;
this->y1=y_floor;
this->y2=y_ceiling;
this->z1=z1;
this->z2=z2;
}

#endif

//控件.hpp

#ifndef _MACRO_CONTROLS_C
#define _MACRO_CONTROLS_C

#include <vector>

#include "Wall.hpp"
...
#endif

//控件.cpp

#ifndef _MACRO_CONTROLS_C
#define _MACRO_CONTROLS_C


// Include GLFW
#include <GL/glfw.h>

// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;

#include <iostream>

#include "controls.hpp"

#include <unistd.h>

#define GetCurrentDir getcwd

#include "Wall.hpp"
...
#endif

// main.cpp

// Include standard headers
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <list>
#include <vector>
#include <time.h>
#include <typeinfo>

// Include GLEW
#include <GL/glew.h>

// Include GLFW
#include <GL/glfw.h>

// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;

#include <unistd.h>
#define GetCurrentDir getcwd

#include <common/shader.hpp>
#include <common/texture.hpp>
#include <common/controls.hpp>
#include "Player.cpp"
#include "Turret.hpp"

#include "Wall.hpp"

在我将 #include "Wall.hpp"放入文件 controls.hpp 或/和 controls.cpp 之前,代码运行良好。我面临错误“重新定义 Wall”,并且在此之后“Gl.h included before glew.h”。

最佳答案

我认为问题在于您在 controls.cpp 中包含 header 的方式:

Wall.hpp -> 类 WALL 的定义

controls.hpp -> 包含 wall.hpp

controls.cpp -> 包含 wall.hpp 和 controls.hpp

所以基本上在 controls.cpp 中你包含了 WALL 类的定义符两次;这可能是您出错的原因。如果你只包含 controls.hpp,你应该摆脱错误

关于即使使用 ifndef,C++ 重定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20653684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com