'对象':' struct / class'类型重新定义WITH标题保护。怎么修?

时间:2014-12-25 08:01:36

标签: c++ c-preprocessor sfml

尽管有单独的编译,我制作了一个对象的头文件和源文件。但是,当我编译代码时,我收到错误

  

错误C2011:' Snake' :' struct'类型重新定义

我已经在线查看了这个错误的解决方案,并且所有人都说这是因为没有添加标题保护以防止多个包含。然而,情节扭曲是即使我添加这些警卫,错误仍然存​​在。显然有一些我没有意识到的事情。那么问题是什么?这是我的代码。

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; }
};

2 个答案:

答案 0 :(得分:2)

struct Snake {}文件中.cpp的定义正在重新定义.h文件中较早的文件。这就是你收到错误的原因。

答案 1 :(得分:2)

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

在标题中:

struct Example
{
void foo();
};

在cpp:

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