gpt4 book ai didi

c++ - Allgro 5 'multiple definition of … first defines here …'中的定义错误

转载 作者:行者123 更新时间:2023-12-03 07:39:30 24 4
gpt4 key购买 nike

我尝试编译我的allegro 5程序,但是由于某种原因我得到了它
我想制作一种类似于引擎的结构,所以我制作了一个应用类,以便更轻松地制作像ludum这样的游戏
因此,您将创建一个类并使其从应用程序继承,然后只需填写更新init和draw方法
我真的是从终端进行编译的初学者(我使用linux)
我的构建脚本如下所示
gcc src/*。cpp build/Game $ {pkg-config allegro-5 allegro_font-5 --libs --cflags)
./build/Game
暂停
希望我得到答案

/usr/bin/ld: /tmp/ccbdAHA8.o: in function `Pislify::DefaultWindowConfig()':
main.cpp:(.text+0x0): multiple definition of `Pislify::DefaultWindowConfig()'; /tmp/ccwYzbn9.o:Engine.cpp:(.text+0x0): first defined here
/usr/bin/ld: /tmp/ccwYzbn9.o:(.data.rel.ro._ZTIN7Pislify4GameE[_ZTIN7Pislify4GameE]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
collect2: error: ld returned 1 exit status
我的代码:
Engine.hpp
#ifndef ENGINE_HPP
#define ENGINE_HPP

#include <allegro5/allegro5.h>
#include <allegro5/allegro_font.h>

namespace Pislify
{
struct WindowConfiguration
{
int WindowWidth;
int WindowHeight;
char* WindowTitle;
int FPS;
};
WindowConfiguration DefaultWindowConfig()
{
WindowConfiguration conf;
conf.WindowWidth = 1280;
conf.WindowHeight = 720;
conf.FPS = 60;
conf.WindowTitle = "game";
}
class Game
{
WindowConfiguration config;
void Start();

ALLEGRO_TIMER* timer;
ALLEGRO_EVENT_QUEUE* queue;
ALLEGRO_DISPLAY* disp;
ALLEGRO_FONT* font;

public:
ALLEGRO_EVENT event;
Game(WindowConfiguration c);
Game(int w,int h,char* t);
virtual void Init(){}
virtual void Update(){}
virtual void Draw(){}
};
}

#endif
engine.cpp
#include "Engine.hpp"
using namespace Pislify;
void Game::Start()
{
//Initialize allegro
al_init();
al_install_keyboard();

//initalize Allegro's Vars
timer = al_create_timer(1.0 / config.FPS);
queue = al_create_event_queue();
disp = al_create_display(config.WindowWidth, config.WindowHeight);
font = al_create_builtin_font();

//some more al config
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_display_event_source(disp));
al_register_event_source(queue, al_get_timer_event_source(timer));

//game loop
Init();

bool ShouldClose;
al_start_timer(timer);

while(!ShouldClose)
{
al_wait_for_event(queue, &event);

Update();

//time to draw? draw!
if(event.type == ALLEGRO_EVENT_TIMER)
{
Draw();
}

//close Window?
if((event.type == ALLEGRO_EVENT_DISPLAY_CLOSE))
{
break;
}
}
al_destroy_font(font);
al_destroy_display(disp);
al_destroy_timer(timer);
al_destroy_event_queue(queue);
}

Game::Game(WindowConfiguration c)
{
config = c;
Start();
}
main.cpp
#include "Engine.hpp"

using namespace Pislify;
int main()
{
Game g(DefaultWindowConfig());
return 0;
}

最佳答案

由于要在多个源文件中包含Engine.hpp,因此必须将其标记为inline。这样做将允许多次定义该函数。

namespace Pislify
{
//...
inline WindowConfiguration DefaultWindowConfig()
{
WindowConfiguration conf;
conf.WindowWidth = 1280;
conf.WindowHeight = 720;
conf.FPS = 60;
conf.WindowTitle = "game";
}
//...
https://www.cplusplus.com/articles/2LywvCM9/的报价 5. By marking it as inline, you can put a function definition in a header file (i.e. it can be included in multiple compilation unit, without the linker complaining)

关于c++ - Allgro 5 'multiple definition of … first defines here …'中的定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64766800/

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