gpt4 book ai didi

c++ - '对象' : 'struct/class' type redefinition WITH header guards. 如何修复?

转载 作者:行者123 更新时间:2023-11-30 01:50:15 25 4
gpt4 key购买 nike

尽管分开编译,我还是制作了一个对象的头文件和源文件。但是,当我编译代码时,出现错误

error C2011: 'Snake' : 'struct' type redefinition

我在网上看了这个错误的解决办法,都说是因为没有添加header guard来防止多重包含。然而,情节转折是即使我添加了这些守卫,错误仍然存​​在。显然有些事情我没有意识到。那么问题是什么?这是我的代码。

Snake.h

#ifndef GUARD_SNAKE_H
#define GUARD_SNAKE_H

#include "variables.h"
#include "SFML/Graphics.hpp"

struct Snake
{
public:
sf::RectangleShape rect;
bool goingUp;
bool goingDown;
bool goingLeft;
bool goingRight;
//
bool realUp;
bool realDown;
bool realLeft;
bool realRight;
//
bool secInput;
bool secUp;
bool secDown;
bool secLeft;
bool secRight;

int x[allDots];
int y[allDots];
void changeDirection(int);
void input();
void moveUpdate();
void checkReals();
std::vector<sf::RectangleShape> draw();
bool goingHorizontal();
bool goingVertical();
};

#endif

Snake.cpp

#include "Snake.h"
#include "variables.h"
#include "SFML/Graphics.hpp"
#include <iostream>
#include <array>

struct Snake {
sf::RectangleShape rect;
bool goingUp;
bool goingDown;
bool goingLeft;
bool goingRight;
//
bool realUp;
bool realDown;
bool realLeft;
bool realRight;
//
bool secInput;
bool secUp;
bool secDown;
bool secLeft;
bool secRight;

int x[allDots];
int y[allDots];

//Snake Constructor: int coordinates for position
Snake::Snake(){
for (int z = 0; z < dots; z++){
this->x[z] = windowWidth / 2 - z * dotSize;
this->y[z] = windowHeight / 2;
}

//Default direction
goingUp = false;
goingDown = false;
goingLeft = false;
goingRight = true;

realUp = false;
realDown = false;
realLeft = false;
realRight = false;

secUp = false;
secDown = false;
secLeft = false;
secRight = false;

secInput = false; //Variable indicating first input has changed and able to declare to second input variables.
}

/*Changing direction member function*/
void Snake::changeDirections(int dir){
if(secInput == false){
if(secUp) {
goingUp = true;
secUp=false;
}
else if(secDown) {
goingDown=true;
secDown=false;
}
else if (secLeft) {
goingLeft=true;
secLeft = false;
}
else if (secRight) {
goingRight=true;
secRight=false;
} else{
switch (dir){
case 1:
goingUp = true;
goingDown = false;
goingLeft = false;
goingRight = false;
break;
case 2:
goingUp = false;
goingDown = true;
goingLeft = false;
goingRight = false;
break;
case 3:
goingUp = false;
goingDown = false;
goingLeft = true;
goingRight = false;
break;
case 4:
goingUp = false;
goingDown = false;
goingLeft = false;
goingRight = true;
break;
}
}
secInput = true;
} else {
switch (dir){
case 1:
secUp = true;
secDown = false;
secLeft = false;
secRight = false;
break;
case 2:
secUp = false;
secDown = true;
secLeft = false;
secRight = false;
break;
case 3:
secUp = false;
secDown = false;
secLeft = true;
secRight = false;
break;
case 4:
secUp = false;
secDown = false;
secLeft = false;
secRight = true;
break;
}
}
}
void Snake::input(){
//std::cout<<"Keyboard Input Called" <<std::endl;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && !realDown)
changeDirections(1);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S) && !realUp)
changeDirections(2);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A) && !realRight)
changeDirections(3);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D) && !realLeft)
changeDirections(4);

}
/*Frame by frame update*/

void Snake::moveUpdate(){

for (int z = dots; z > 0; z--){
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
}

/*Movement actions*/
if (goingUp)
y[0] -= dotSize;

else if (goingDown)
y[0] += dotSize;

else if (goingLeft)
x[0] -= dotSize;

else if (goingRight)
x[0] += dotSize;
}

void Snake::checkReals(){
if (x[0] < x[1]){
realLeft = true;
realRight = false;
realUp = false;
realDown = false;
}
if (x[0] > x[1]){
realRight = true;
realLeft = false;
realUp = false;
realDown = false;
}
if (y[0] < y[1]){
realUp = true;
realDown = false;
realRight = false;
realLeft = false;
}
if (y[0] > y[1]){
realDown = true;
realUp = false;
realLeft = false;
realRight = false;
}
else
std::cerr << "";//"Error Mismatch" << std::endl;

//Debug Output
if(goingUp)
std::cout<< "First Direction: goingUp" << std::endl;
if(goingDown)
std::cout<< "First Direction: goingDown" << std::endl;
if(goingLeft)
std::cout<< "First Direction: goingLeft" << std::endl;
if(goingRight)
std::cout<< "First Direction: goingRight" << std::endl;
if(secUp)
std::cout<< "First Direction: secUp" << std::endl;
if(secDown)
std::cout<< "First Direction: secDown" << std::endl;
if(secLeft)
std::cout<< "First Direction: secLeft" << std::endl;
if(secRight)
std::cout<< "First Direction: secRight" << std::endl;
}

std::vector<sf::RectangleShape> Snake::draw(){
std::vector<sf::RectangleShape> recvec;
for (int z = dots; z >= 0; z--){
sf::RectangleShape r;
r.setPosition(x[z], y[z]);
r.setSize(sf::Vector2f( dotSizeF, dotSizeF ));
r.setFillColor(sf::Color::Green);
if (z == 0)
r.setFillColor(sf::Color::Yellow);
r.setOrigin(dotSize / 2.f, dotSize / 2.f);
recvec.push_back(r);
}
return recvec;
}

bool Snake::goingHorizontal(){ return goingLeft || goingRight; }
bool Snake::goingVertical(){ return goingDown || goingUp; }
};

最佳答案

您有重定义错误,因为您的 cpp 文件中确实有一个重定义:struct Snake{...}; 部分。这不是您为结构的方法提供定义的方式。这是一个简化的示例:

在标题中:

struct Example
{
void foo();
};

在 cpp 中:

void Example::foo()
{
//foo stuff
}

关于c++ - '对象' : 'struct/class' type redefinition WITH header guards. 如何修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27645356/

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