Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » |multiple definition of `Player::test()' Error

This thread is locked; no one can reply to it. rss feed Print
|multiple definition of `Player::test()' Error
Eric Johnson
Member #14,841
January 2013
avatar

Hi there.

I recently managed to compile and build Allegro 5 data on my Windows 7 machine (I primarily use Linux Ubuntu). I have a very simple, very sloppy test project established that is returning errors: "C:\Users\Eric II\Desktop\Games\allegro\Allegro\player.cpp|3|multiple definition of `Player::test()'|".

main.cpp

#SelectExpand
1#include "allegro5/allegro.h" 2#include "allegro5/allegro_image.h" 3#include "player.cpp" 4#include <iostream> 5 6using namespace std; 7 8int main() { 9 10 al_init(); 11 12 Player Player; 13 14 bool done = false; 15 16 ALLEGRO_DISPLAY *display = al_create_display(360, 240); 17 18 al_set_window_title(display, "Mundane"); 19 20 al_init_image_addon(); 21 22 // Directory before change 23 std::cout << al_get_current_directory() << endl; 24 25 ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 26 al_append_path_component(path, "data"); 27 al_change_directory(al_path_cstr(path, '/')); 28 29 // New directory 30 std::cout << al_get_current_directory() << endl; 31 32 ALLEGRO_BITMAP *player = al_load_bitmap("player.png"); 33 34 if (!player) { 35 36 done = true; 37 std::cout << "Failed to load player!" << endl; 38 } 39 40 while (!done) { 41 42 //Player.draw(); 43 44 al_flip_display(); 45 al_clear_to_color(al_map_rgb(255, 255, 255)); 46 47 done = true; 48 } 49 50 al_rest(3); 51 52 al_destroy_display(display); 53 al_destroy_bitmap(player); 54 55 return 0; 56}

player.h

class Player {

    public:
            void test();
};

player.cpp

#include "player.h"

void Player::test() {

}

Any ideas why this is happening? I appreciate your assistance. :)

Arthur Kalliokoski
Second in Command
February 2005
avatar

I'm ignorant of C++ classes and such, but don't you have to use header guards in C++?

#ifndef _PLAYER_H_
#define _PLAYER_H_

class Player {

    public:
            void test();
};

#endif  //Now _PLAYER_H_ (and the Player class) is defined only once.

There are variations of this, such as #pragma once.

They all watch too much MSNBC... they get ideas.

Eric Johnson
Member #14,841
January 2013
avatar

I tried that, but to no avail. :-/

Allow me to ask you this: why is it that my code compiles properly on Linux Ubuntu (as well as Debian), yet does not on Windows?

SiegeLord
Member #7,827
October 2006
avatar

Err... why are you including "player.cpp"?

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

pkrcel
Member #14,001
February 2012

#include "player.cpp"

shouldn't this include the header file player.h instead of player.cpp?

I am not entirely sure that this is an actual problem but smells fishy, you only need to include the .h file I guess.

Also, are the player.h and .cpp you provided only tidbits of the files or are them the whole thing?

EDIT: yeah what Siegelord said.of course. :D

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Eric Johnson
Member #14,841
January 2013
avatar

Oh snap... I've been going about this the wrong way for a while then. Including the header file opposed to the cpp file worked. I appreciate it guys. :D

Go to: