C++多个定义

C++ Multiple Definitions

本文关键字:定义 C++      更新时间:2023-10-16

我正在尝试构建这些文件,但它给了我一个多重定义错误。

主.cpp:

#include "SDL/SDL.h"
#include "Core.h"
#include "GameStates.h"
#include "globals.h"
int main(int argc, char** args) 
{
if(core.Initilization(640, 480, 32, SDL_SWSURFACE) == -1)
{
    SDL_Quit();
}
while(core.desiredstate != core.quit)
{
    ::currentstate->EventHandling();
    ::currentstate->Logic();
    core.ChangeState();
    ::currentstate->Render();
    ::currentstate->Update();
}
SDL_FreeSurface(core.screen);
SDL_Quit();

}

核心.cpp:

#include "Core.h"
#include "GameStates.h"
#include "SDL/SDL.h"
#include "Intro.h"
#include "globals.h"
#include <string>
/* Starts SDL subsystems and sets screen attributes */
bool Core::Initilization(int SCREEN_WIDTH, int SCREEN_HEIGHT, int SCREEN_BPP, int FLAGS) 
{
    //starts SDL subsystems, returns false upon error
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
    return false;
}
//The screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, FLAGS);
//Returns false if there was an error
if(screen == NULL)
{
    return false;
}
SDL_WM_SetCaption("Game", NULL);
return true;
}
/* Loads an image and optimizes it */
SDL_Surface* Core::Load(std::string filename)
{
//original loaded image
SDL_Surface* original = SDL_LoadBMP(filename.c_str());
SDL_Surface* optimized = NULL;
if(original != NULL)
{
    //Sets optimized to optimized version of original
    optimized = SDL_DisplayFormat(original);
    SDL_FreeSurface(original);
}
return optimized;
} 
/* Blits surfaces */
void Core::ApplySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination) 
{
//holds the x y coordinates
SDL_Rect location;
location.x = x;
location.y = y;
if(destination != NULL) 
{
    SDL_BlitSurface(source, NULL, destination, &location);
}
}
/* Sets desiredstate to be used in ChangeState(); */
void Core::SetState(int newstate) 
{
if(desiredstate != state_null && desiredstate != quit)
{
    desiredstate = newstate;
}
}
/* Changes the game state */
void Core::ChangeState()
{
    if(desiredstate != state_null && desiredstate != quit)
    {
        //frees old state memory
    delete ::currentstate;
    switch(desiredstate)
    {
        case intro:
            //allocates new state memory
            ::currentstate = new Intro();
        break;
    }
    stateID = desiredstate;
    desiredstate = state_null;
}
}

GameStates.h:

#ifndef GAMESTATES_H
#define GAMESTATES_H
class GameStates 
{
public:
    virtual void EventHandling() = 0;
    virtual void Logic() = 0;
    virtual void Render() = 0;
    virtual void Update() = 0;
};
#endif

简介:

#ifndef INTRO_H
#define INTRO_H
#include "SDL/SDL.h"
#include "GameStates.h"
class Intro : public GameStates
{
    private:
    SDL_Surface* test;
public:
    Intro();
    void EventHandling();
    void Logic();
    void Render();
    void Update();
    ~Intro();
} intro;
#endif

简介.cpp:

#include "Intro.h"
#include "GameStates.h"
#include "Core.h"
#include "SDL/SDL.h"
Intro::Intro()
{
test = core.Load("test.bmp");
}
void Intro::EventHandling()
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
    switch(event.type)
    {
        case SDL_QUIT:
            core.SetState(core.quit);
        break;
    }
}
}
void Intro::Logic()
{
//to be coded when the program actually builds...
}
void Intro::Render()
{
core.ApplySurface(30, 30, test, core.screen);
}
void Intro::Update()
{
SDL_Flip(core.screen);
}
Intro::~Intro()
{
SDL_FreeSurface(test);
}

globals.h:

#include "GameStates.h"
#include "SDL/SDL.h"
GameStates* currentstate = NULL;

抱歉,如果缩进关闭;必须放置四个空格才能将其视为代码块,这有点混乱。

下面是错误消息:

/tmp/ccWxKsO5.o:(.bss+0x0): multiple definition of `core'
/tmp/cc13Eqmt.o:(.bss+0x0): first defined here
/tmp/ccWxKsO5.o:(.bss+0x20): multiple definition of `currentstate'
/tmp/cc13Eqmt.o:(.bss+0x10): first defined here
/tmp/ccJXxewI.o:(.bss+0x0): multiple definition of `intro'
/tmp/ccWxKsO5.o:(.bss+0x10): first defined here
/tmp/ccJXxewI.o:(.bss+0x10): multiple definition of `core'
/tmp/cc13Eqmt.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status

制作文件:

OBJS = main.o Intro.o Core.o
CC = g++
DEBUG = -g
CFLAGS = -Wall -c $(DEBUG)
LIBS = -lSDL
game : $(OBJS)
    $(CC) $(CFLAGS) $(OBJS) -o game $(LIBS)
main.o : Core.h GameStates.h globals.h 
    $(CC) $(CFLAGS) main.cpp $(LIBS)
Core.o : Core.h Core.cpp GameStates.h Intro.h globals.h
    $(CC) $(CFLAGS) Core.cpp $(LIBS)
Intro.o : Intro.cpp GameStates.h Core.h
    $(CC) $(CFLAGS) Intro.cpp $(LIBS)

问题不在于你的代码,而在于你的构建系统。

任何健全的构建系统都将目标文件的名称与源文件的名称相匹配。 但是你有ccWxKsO5.occ13Eqmt.o. 更糟糕的是,构建系统似乎试图链接从同一源生成的多个对象(也许有些对象是由早期运行的编译器创建的(。

tempnam和通配*.o不是构建C++程序的合理方法。


好吧,可能也存在一些代码问题。 但是,一旦错误消息中的对象名称与源文件相关,查找和修复这些将容易一千倍。

对于在多个翻译单元之间共享的对象,规则是:必须只有一个定义,但您可能有多个声明。

实际上,这意味着:将"extern Class object;"放在您的.h文件中,将"类对象;"放在您的一个.CPP 文件。

例如,对于intro,请将您的 Intro.h 更改为:

class Intro : public GameStates
{
  ... // whatever 
};
extern Intro intro;

并将此行添加到介绍.cpp:

Intro intro;

同样,对于 currentstate ,在 globals.h 中:

extern GameStates* currentstate;

在一个.CPP(编译器对哪一个无关紧要(:

GateStates* currentstate = NULL;

附言:你的制作文件坏了。您传递-c,这意味着"不链接"到您的链接步骤。试试这个:

OBJS = main.o Intro.o Core.o
CC = g++
DEBUG = -g
CFLAGS = -Wall $(DEBUG)
LIBS = -lSDL
game : $(OBJS)
    $(CC) $(CFLAGS) $(OBJS) -o game $(LIBS)
main.o : Core.h GameStates.h globals.h 
    $(CC) -c $(CFLAGS) main.cpp
Core.o : Core.h Core.cpp GameStates.h Intro.h globals.h
    $(CC) -c $(CFLAGS) Core.cpp
Intro.o : Intro.cpp GameStates.h Core.h
    $(CC) -c $(CFLAGS) Intro.cpp

在 globals.h 中,必须声明currentstate extern。然后创建全局.cpp,定义为 ( GameStates* currentstate = NULL; (。我在您的代码中找不到任何介绍或核心的参考,但这可能是同一个问题:只要您在 extern 中声明全局变量,您就可以随时声明全局变量,并且每个生成的二进制文件只定义它们一次,只有一个翻译单元。

此外,您可能希望向 globals.h 添加一个标头保护 (#ifndef GLOBALS_H ...(,以防万一您在其中添加任何其他内容。

把 include guards in globals.h使游戏状态*声明成为外部

//globals.h
#ifndef _MY_GLOBALS_H_
#define _MY_GLOBALS_H_
#include "GameStates.h"
#include "SDL/SDL.h"
extern GameStates* currentstate;
#endif

主.cpp

#include "globals.h"
GameStates* currentState = 0;