Multi link errors

This is a discussion on Multi link errors within the C++ Programming forums, part of the General Programming Boards category; I am starting a large application involving classes and different files. I have created a player class and set it ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Multi link errors

    I am starting a large application involving classes and different files.

    I have created a player class and set it as a header file so i can use it in my other files without having to "extern" call all the variables,

    The class name and the object player have both been defined in the main() function in the first cpp file.

    But when i went to create my next cpp file, I included my file that has the player class like this:

    [code]#include "player.h"[\code]

    and when i went to compile i got 15 linker errors up saying stuff like:

    error: 'int health' multiple definiotion - first defined here

    I though that if i placed my class in a header file then i could just include it with my other files... what am i doing wrong?

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    the the top of your header file include
    #ifndef MYHEADER_H
    #define MYHEADER_H

    then at the end add
    #endif /* MYHEADER_H */

    (and change MYHEADER_H to something more relevent to file)

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Here is my header file for my Player Class

    Code:
    #ifndef PLAYER_H
    #define PLAYER_H
    
    #include <string>
    
    using std::string;
    
    class Player
    {
     	  public:
     		 int hp;
     		 int score;
     		 int level;
     		 
     		 Player ( int h, int s, int l );
    	  		 
          private:
      		  bool palive;
      		  bool pdead;
    };
    
    Player::Player ( int h, int s, int l )
    {
         hp = h;
         score = s;
         level = l;
         
         palive = true;
         pdead = false;
    }
    
    #endif
    Error I get when including it in my new cpp file:

    multiple definition of `Player::Player(int, int, int)'
    first defined here 'LINKER ERROR MSVC++2003

    It works fine included in the file with main()

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Code:
    #ifndef PLAYER_H
    #define PLAYER_H
    
    #include <string>
    
    using std::string;
    
    class Player
    {
     	  public:
     		 int hp;
     		 int score;
     		 int level;
     		 
     		 Player ( int h, int s, int l );
    	  		 
          private:
      		  bool palive;
      		  bool pdead;
    };
    
    //!! Move this function to player.cpp, and #include player.h in that file as well
    //!! You're getting a copy of this function in all your .cpp files, so it becomes multiply declared.
    Player::Player ( int h, int s, int l )
    {
         hp = h;
         score = s;
         level = l;
         
         palive = true;
         pdead = false;
    }
    
    #endif

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The only functions you should put into header files are inline functions, which have to be declared in every file they're used in.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List, Please Help!
    By CodeMonkeyZ in forum C Programming
    Replies: 5
    Last Post: 02-17-2009, 05:23 AM
  2. Winsock compilation errors
    By jmd15 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-03-2005, 08:00 AM
  3. SDL Link Errors.
    By suzakugaiden in forum Game Programming
    Replies: 2
    Last Post: 06-15-2005, 05:40 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 12:43 PM

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