26

我一直在训练使用 C++ 中的面向对象编程,但我不断收到此错误:

1>main.obj : error LNK2005: "int WIDTH" (?WIDTH@@3HA) already defined in GameObject.obj
1>main.obj : error LNK2005: "int HEIGHT" (?HEIGHT@@3HA) already defined in GameObject.obj
1>Spaceship.obj : error LNK2005: "int WIDTH" (?WIDTH@@3HA) already defined in GameObject.obj
1>Spaceship.obj : error LNK2005: "int HEIGHT" (?HEIGHT@@3HA) already defined in GameObject.obj
1>C:\Users\ted\documents\visual studio 2010\Projects\fullSpace\Debug\fullSpace.exe : fatal error LNK1169: one or more multiply defined symbols found

但是对我来说,似乎整个代码都写得很好,两个整数只在 Global 标头中提到,所有对象似乎都在正确继承。但是,就像我刚才说的,我是 OOP 的初学者,所以我真的需要一个意见:还值得一提的是,我正在使用 allegro 5 来创建一个侧面射手。

这是代码:

(主要的):

#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_audio.h>
#include <allegro5\allegro_acodec.h>

#include <list>


#include "GameObject.h"
#include "Spaceship.h"
#include "Globals.h"



//controls

bool keys[] = {false, false, false, false, false};
enum KEYS{UP, DOWN, LEFT, RIGHT, SPACE};

//globals
Spaceship *ship;

std::list <GameObject *> objects;
std::list <GameObject *>::iterator iter;
std::list <GameObject *>::iterator iter2;



//prototypes



//main function
int main(int argc, char **argv)
{
    //shell variables
    bool done = false;
    bool render = false;

    float gameTime = 0;
    int frames = 0;
    int gameFPS = 0;

    //project variables

    ship = new Spaceship();


    ALLEGRO_BITMAP *shipImage = NULL;
    ALLEGRO_BITMAP *cometImage= NULL;
    ALLEGRO_BITMAP *explImage = NULL;
    ALLEGRO_BITMAP *bgImage = NULL;
    ALLEGRO_BITMAP *mgImage = NULL;
    ALLEGRO_BITMAP *plImage = NULL;
    ALLEGRO_BITMAP *mgImage2 = NULL;
    ALLEGRO_BITMAP *fgImage = NULL;
    ALLEGRO_BITMAP *titleImage= NULL;
    ALLEGRO_BITMAP *lostImage = NULL;


    //allegro variables
    ALLEGRO_DISPLAY *display = NULL;
    ALLEGRO_EVENT_QUEUE *event_queue = NULL;
    ALLEGRO_TIMER *timer;
    ALLEGRO_FONT *font18;



    //initiate variables
    if(!al_init())
        return -1;

    display = al_create_display(WIDTH, HEIGHT);
    if(!display)
    return -1;

    //addon installation

    al_install_keyboard();
    al_init_image_addon();
    al_init_font_addon();
    al_init_ttf_addon();
    al_init_primitives_addon();
    al_install_audio();
    al_init_acodec_addon();

    //project init

    font18 = al_load_font("arial.ttf", 18, 0);
    al_reserve_samples(15);




    bgImage = al_load_bitmap("layer1.png");
    mgImage = al_load_bitmap("layer2.png");
    plImage = al_load_bitmap("starMG.png");
    mgImage2 = al_load_bitmap("layer3.png");
    fgImage = al_load_bitmap("layer4.png");




    shipImage = al_load_bitmap("spaceship.png");
    al_convert_mask_to_alpha(shipImage, al_map_rgb(255, 0, 255));



    cometImage = al_load_bitmap("asteroid-1-96.png");
    explImage = al_load_bitmap("explosion_3_40_128.png");

    titleImage = al_load_bitmap("Shooter_Title.png");
    lostImage = al_load_bitmap("Shooter_Lose.png");



    //object init
    ship->init(shipImage);


    //iter list
    objects.push_back(ship);


    srand(time(NULL));

    //timer init and startup

    event_queue = al_create_event_queue();
    timer = al_create_timer(1.0 / 60);

    al_register_event_source(event_queue, al_get_timer_event_source(timer));
    al_register_event_source(event_queue, al_get_keyboard_event_source());

    al_start_timer(timer);
    gameTime = al_current_time();

    while(!done)
    {
        ALLEGRO_EVENT ev;
        al_wait_for_event(event_queue, &ev);

        //input
        if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
        {
            switch(ev.keyboard.keycode)
            {
            case ALLEGRO_KEY_ESCAPE:
                done = true;
                break;
            case ALLEGRO_KEY_LEFT:
                keys[LEFT] = true;
                break;
            case ALLEGRO_KEY_RIGHT:
                keys[RIGHT] = true;
                break;
            case ALLEGRO_KEY_UP:
                keys[UP] = true;
                break;
            case ALLEGRO_KEY_DOWN:
                keys[DOWN] = true;
                break;
            case ALLEGRO_KEY_SPACE:
                keys[SPACE] = true;
                break;


            }
        } else if(ev.type == ALLEGRO_EVENT_KEY_UP)
        {
            switch(ev.keyboard.keycode)
            {
            case ALLEGRO_KEY_ESCAPE:
                done = true;
                break;
            case ALLEGRO_KEY_LEFT:
                keys[LEFT] = false;
                break;
            case ALLEGRO_KEY_RIGHT:
                keys[RIGHT] = false;
                break;
            case ALLEGRO_KEY_UP:
                keys[UP] = false;
                break;
            case ALLEGRO_KEY_DOWN:
                keys[DOWN] = false;
                break;
            case ALLEGRO_KEY_SPACE:
                keys[SPACE] = false;
                break;
            }
        }



        else if (ev.type == ALLEGRO_EVENT_TIMER)
        {
            render = true;

            //fps
            frames++;
            if(al_current_time() - gameTime >= 1)
            {
                gameTime = al_current_time();
                gameFPS = frames;
                frames = 0;
            }

            //shipUpdate

            if(keys[UP])
                ship ->moveUp();
            else if(keys[DOWN])
                ship ->moveDown();
            else
                ship->resetAnim(1);

            if(keys[LEFT])
                ship ->moveLeft();
            else if(keys[RIGHT])
                ship -> moveRight();
            else
                ship ->resetAnim(0);

        }
        //render

            if(render && al_is_event_queue_empty(event_queue))
            {
                render = false;

                //begin render
                for(iter = objects.begin(); iter != objects.end(); ++iter)
                    (*iter)->render();




                //Flip Buffers
                al_flip_display();
                al_clear_to_color(al_map_rgb(0,0,0));
            }
        }

                //destroy objects



        //visual objects
    al_destroy_bitmap(cometImage);
    for(iter = objects.begin(); iter != objects.end(); ++iter)
        (*iter)->destroy(shipImage);
        iter = objects.erase(iter);

    al_destroy_bitmap(explImage);
    al_destroy_bitmap(bgImage);
    al_destroy_bitmap(mgImage);
    al_destroy_bitmap(fgImage);
    al_destroy_bitmap(titleImage);
    al_destroy_bitmap(lostImage);

        //audio objects
    /*
    al_destroy_sample(shot);
    al_destroy_sample(boom);
    al_destroy_sample(song);
    al_destroy_sample_instance(songInstance);
    */


        //shell objects
    al_destroy_font(font18);
    al_destroy_timer(timer);
    al_destroy_event_queue(event_queue);
    al_destroy_display(display);

    return 0;
}

(Globals.h):

#pragma once

int WIDTH = 1024;
int HEIGHT = 800;

enum ID{PLAYER, ENEMY, BULLET, BORDER, MISC};
enum STATES{TITLE, PLAYING, LOST};

(游戏对象.h):

#pragma once


#include "Globals.h"
#include <iostream>

#include <allegro5/allegro5.h>

#include <allegro5/allegro_primitives.h>



class GameObject
{
private:
    int ID;
    bool alive;
    bool collidable;

protected:
    float x;
    float y;

    float velX;
    float velY;

    int dirX;
    int dirY;

    int boundX;
    int boundY;

    int maxFrame;
    int curFrame;
    int frameCount;
    int frameDelay;
    int frameWidth;
    int frameHeight;
    int animationColumns;
    int animationDirection;

    ALLEGRO_BITMAP *image;

public:
    GameObject();
    void virtual destroy(ALLEGRO_BITMAP *image);

    void init(float x, float y, float velX, float velY, int dirX, int dirY, int boundX, int boundY);
    void virtual update();
    void virtual render();

    float getX() {return x;}
    float getY() {return y;}

    void setX(float x) {GameObject::x = x;}
    void setY(float y) {GameObject::y = y;}

    int getBoundX() {return boundX;}
    int getBoundY() {return boundY;}

    int getID() {return ID;}
    void setID(int ID) {GameObject::ID = ID;}

    bool getAlive() {return alive;}
    void setAlive(bool alive) {GameObject::alive = alive;}

    bool getCollidable() {return collidable;}
    void setCollidable(bool collidable) {GameObject::collidable = collidable;}

    bool checkCollisions(GameObject *otherObject);
    void virtual collided(int objectID);
    bool collidableCheck();
};

(游戏对象.cpp):

#include "GameObject.h"

GameObject::GameObject()
{
    x = 0;
    y = 0;

    velX = 0;
    velY = 0;

    dirX = 0;
    dirY = 0;

    boundX = 0;
    boundY = 0;

    maxFrame = 0;
    curFrame = 0;
    frameCount = 0;
    frameDelay = 0;
    frameWidth = 0;
    frameHeight = 0;
    animationColumns = 0;
    animationDirection = 0;

    image = NULL;

    alive = true;
    collidable = true;

}

void GameObject::destroy(ALLEGRO_BITMAP *image)
{
    if(image != NULL)
        al_destroy_bitmap(image);

}
void GameObject::init(float x, float y, float velX, float velY, int dirX, int dirY, int boundX, int boundY)
{
    GameObject::x = x;
    GameObject::y = y;

    GameObject::velX = velX;
    GameObject::velY = velY;

    GameObject::dirX = dirX;
    GameObject::dirY = dirY;

    GameObject::boundX = boundX;
    GameObject::boundY = boundY;

}

void GameObject::update()
{
    x += velX*dirX;
    y += velY*dirY;
}

void GameObject::render()
{

}

bool GameObject::checkCollisions(GameObject *otherObject)
{
    float oX = otherObject->getX();
    float oY = otherObject->getY();

    int obX = otherObject->getBoundX();
    int obY = otherObject->getBoundY();

    if(x + boundX > oX - obX &&
       x - boundX < oX + obX &&
       y + boundY > oY - obY &&
       y - boundY < oY + obY
       )
       return true;
    else
       return false;
}

void GameObject::collided(int objectID)
{

}
bool GameObject::collidableCheck()
{
    return alive && collidable;
}

(太空船.h):

#pragma once

#include "GameObject.h"

class Spaceship : public GameObject
{
private :
    int lives;
    int score;
    int animationRow;

public :
    Spaceship();

    void destroy(ALLEGRO_BITMAP *image);

    void init(ALLEGRO_BITMAP *image = NULL);

    void update();
    void render();

    void moveUp();
    void moveDown();
    void moveLeft();
    void moveRight();

    void resetAnim(int pos);

    int getLives(){return lives;}

    int getScore() {return score;}

    void looseLife() {lives--;}
    void addPoint() {score++;}

    void collide(int objectID);



};

(太空船.cpp):

#include "Spaceship.h"

    Spaceship::Spaceship()
    {}

    void Spaceship::destroy(ALLEGRO_BITMAP *image)
    {
        GameObject::destroy(image);
    }
    void Spaceship::init(ALLEGRO_BITMAP *image)
    {
        GameObject::init(20, 200, 6, 6, 0, 0, 10, 12);

        setID(PLAYER);
        setAlive(true);

        lives = 3;
        score = 0;

        maxFrame = 3;
        curFrame = 0;
        frameWidth = 46;
        frameHeight = 41;
        animationColumns = 3;
        animationDirection = 1;

        animationRow = 1;

        if(image != NULL)
        {
            Spaceship::image = image;
        }
    }

    void Spaceship::update()
    {
        GameObject::update();
        if(x < 0)
            x=0;
        else if ( x > WIDTH)
            x = WIDTH;
        if(y < 0)
            y = 0;
        else if (y > HEIGHT)
            y = HEIGHT;
    }
    void Spaceship::render()
    {
        GameObject::render();

        int fx = (curFrame % animationColumns) *frameWidth;
        int fy = animationRow *frameHeight;

        al_draw_bitmap_region(image, fx, fy, frameWidth, frameHeight,
            x - frameWidth /2, y - frameHeight /2, 0);

    }

    void Spaceship::moveUp()
    {
        animationRow = 0;
        dirY = -1;

    }
    void Spaceship::moveDown()
    {
        animationRow = 2;
        dirY = 1;
    }
    void Spaceship::moveLeft()
    {
        curFrame = 2;
        dirX = -1;
    }
    void Spaceship::moveRight()
    {
        curFrame = 1;
        dirX = 1;
    }

    void Spaceship::resetAnim(int pos)
    {
        if(pos == 1)
        {
            animationRow = 1;
            dirY = 0;
        }
        else
        {
            curFrame = 0;
            dirX = 0;
        }
    }

    void Spaceship::collide(int objectID)
    {
        if(objectID == ENEMY)
            lives--;

    }
4

6 回答 6

60

这两个int变量在头文件中定义。这意味着每个包含标题的源文件都将包含它们的定义(标题包含纯文本)。当然会导致多个定义错误。

你有几个选项来解决这个问题。

  1. 制作变量static( static int WIDTH = 1024;)。它们仍将存在于每个源文件中,但它们的定义在源文件之外将不可见。

  2. 使用extern( extern int WIDTH;) 将它们的定义转换为声明,并将定义放入一个源文件中:int WIDTH = 1024;.

  3. 可能是最好的选择:制作变量const( const int WIDTH = 1024;)。这使得它们static隐含,并且还允许它们用作编译时常量,允许编译器直接使用它们的值,而不是发出代码从变量等中读取它。

于 2013-03-20T07:34:18.290 回答
6

您不能将变量定义放在头文件中,因为这些将成为您包含头文件的所有源文件的一部分。

#pragma once只是为了防止同一源文件中的多个包含,而不是防止多个源文件中的多个包含。

您可以在头文件中声明变量extern,然后在单个源文件中定义它们。或者您可以const在头文件中声明变量,然后编译器和链接器将对其进行管理。

于 2013-03-20T07:29:15.840 回答
1
const int WIDTH = 1024;
const int HEIGHT = 800;
于 2013-03-20T07:30:24.000 回答
0

我在这里回答了一个类似的问题。

在项目的设置中,添加 /FORCE:MULTIPLE到链接器的命令行选项。

来自MSDN:“使用 /FORCE:MULTIPLE 创建一个输出文件,无论 LINK 是否为一个符号找到多个定义。”

这就是程序员所说的“快速而肮脏”的解决方案,但有时您只是希望构建完成并稍后解决问题的根源,因此这是一种临时解决方案。要真正避免此错误,前提是您想要

int WIDTH = 1024;
int HEIGHT = 800;

要在多个源文件之间共享,只需在单个 .c / .cpp 文件中声明它们,并在头文件中引用它们:

extern int WIDTH;
extern int HEIGHT;

然后将标题包含在您希望这些全局变量可用的任何其他源文件中。

于 2020-10-25T18:53:08.447 回答
0

In my case I got error message one or more multiply defined symbols found, and I find another error message shows xxx function is already defined in ooo.dll, but in my project doensn't reference ooo.dll at all.

Then I recall I use vcpkg to install ooo.dll possibly globally before, so use vcpkg to remove uninstall ooo.dll package, and build again, the error is solved and build success.

于 2021-07-10T03:03:47.080 回答
-1

只需添加 /FORCE 作为链接器标志,就可以了。

例如,如果您正在处理 CMakeLists.txt。然后添加以下行:

SET(CMAKE_EXE_LINKER_FLAGS  "/FORCE")
于 2019-05-09T07:19:32.123 回答