Class Errors (LNK2005)

This is a discussion on Class Errors (LNK2005) within the C++ Programming forums, part of the General Programming Boards category; I'm getting errors while trying to compile code using 2 classes, box and player. While looking for help online, I ...

  1. #1
    NMZ
    NMZ is offline
    Registered User
    Join Date
    Feb 2005
    Posts
    2

    Class Errors (LNK2005)

    I'm getting errors while trying to compile code using 2 classes, box and player. While looking for help online, I got the feeling that this is a fairly common problem, or the solution is at least obvious to the experienced programmer. Any help?

    I'm getting the following errors:
    Code:
    player.obj : error LNK2005: "float  posX" (?posX@@3MA) already defined in box.obj
    Debug/Text1.exe : fatal error LNK1169: one or more multiply defined symbols found
    Here are code segments pertaining to the error:

    player.h
    Code:
    #ifndef player_h
    #define player_h
    
    class player{
    private:
    	float posX;
    
    public:
    	player();
    	float get_posX() {return posX;}
    };
    
    #endif
    player.cpp
    Code:
    #include "player.h"
    
    float posX = 100.0f;
    
    player::player(){
     // code removed
    }
    box.h
    Code:
    #ifndef box_h
    #define box_h
    
    class box{
    private:
    	float posX;
    
    public:
    	box();
    	box(float x,float y,float z,float w,float h,float l);
    	float get_posX() {return posX;}
    };
    
    #endif
    box.cpp
    Code:
    #include "box.h"
    
    float posX = 0.0f;
    
    box::box(float x,float y,float z,float w,float h,float l){
    // code removed
    }
    
    box::box(){
    // code removed
    }
    Thanks!
    Last edited by NMZ; 02-28-2005 at 12:58 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    I'm guessing the linker is having problem with the following two definitions of posX defined in your two source files. Should these be in a constructor somewhere? Looks to me like they should.

    Code:
    #include "player.h"
    
    float posX = 100.0f;
    
    player::player(){
     // code removed
    }
    Code:
    #include "box.h"
    
    float posX = 0.0f;
    
    box::box(float x,float y,float z,float w,float h,float l){
    // code removed
    }
    
    box::box(){
    // code removed
    }

    I'm would say put the initialization of the posX variable into the constructors.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Its because you have posX globally defined in box.cpp and then you have it globally defined in player.cpp as well, thus the redefinition error. It's not sure which one to use on a global scale.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    What are you attempting to do with the posX you declare in the cpp files? If it is an attempt at initializing the member posX to a default value in the classes, it won't work the way you have it. That should be done in the constructor using an initializing list like this.

    Code:
    #include "player.h"
    
    player::player() : posX (100.0f)
    {
       // code removed
    }
    You're only born perfect.

  5. #5
    NMZ
    NMZ is offline
    Registered User
    Join Date
    Feb 2005
    Posts
    2
    Thanks a ton, guys, you solved the problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 06:16 AM
  4. errors in my class....
    By o0obruceleeo0o in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2003, 03:22 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21