Thread: Unresolved external symbols

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Unresolved external symbols

    Code:
    main.obj||error LNK2019: unresolved external symbol "public: void __thiscall player::transfer(class player &)" (?transfer@player@@QAEXAAV1@@Z) referenced in function _main|
    main.obj||error LNK2019: unresolved external symbol "public: void __thiscall swordsman::AI(class player &)" (?AI@swordsman@@QAEXAAVplayer@@@Z) referenced in function _main|
    main.obj||error LNK2019: unresolved external symbol "public: bool __thiscall player::useMW(void)" (?useMW@player@@QAE_NXZ) referenced in function _main|
    main.obj||error LNK2019: unresolved external symbol "public: bool __thiscall player::useHeal(void)" (?useHeal@player@@QAE_NXZ) referenced in function _main|
    main.obj||error LNK2019: unresolved external symbol "public: void __thiscall player::isDead(void)" (?isDead@player@@QAEXXZ) referenced in function _main|
    main.obj||error LNK2019: unresolved external symbol "void __cdecl showinfo(class player &,class player &)" (?showinfo@@YAXAAVplayer@@0@Z) referenced in function _main|
    main.obj||error LNK2019: unresolved external symbol "public: bool __thiscall player::death(void)" (?death@player@@QAE_NXZ) referenced in function _main|
    main.obj||error LNK2019: unresolved external symbol "public: void __thiscall player::reFill(void)" (?reFill@player@@QAEXXZ) referenced in function _main|
    main.obj||error LNK2019: unresolved external symbol "public: __thiscall swordsman::swordsman(int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0swordsman@@QAE@HV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main|
    C:\Users\Geft\Desktop\06\ex3\main.exe||fatal error LNK1120: 9 unresolved externals|
    ||=== Build finished: 10 errors, 0 warnings (0 minutes, 0 seconds) ===|
    I have the following files where the headers are class declarations and the corresponding cpp files are the definitions:

    Code:
    container.cpp
    container.h
    player.cpp
    player.h includes container.h
    main.cpp includes swordsman.h
    swordsman.cpp
    swordsman.h includes player.h
    What could be the problem?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Search through your source files for definitions (ie implementations) of the functions that the linker is complaining about.

    You will either find that some source files are incomplete (for example, player::transfer() is not defined any of your source files) or that you have left some files out of your project.

    Declaring functions (i.e. prototypes) will get you past the compiler, but the linker is where the rubber hits the road: the linker needs all functions that get called anywhere in your program to be defined, not just declared, otherwise it can't create the executable.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    For instance, here are the functions for player::death and player:isDead as defined in player.cpp:

    Code:
    // report whether character is dead
    bool player::death()
    {
    	return playerdeath;
    }
    
    // check whether character is dead
    void player::isDead()
    {
    	if(HP<=0)		// HP less than 0, character is dead
    	{
    		cout<<name<<" is Dead." <<endl;
    		system("pause");
    		playerdeath=1;	// give the label of death value 1
    	}
    }
    Along with the prototypes:

    Code:
    #ifndef _PLAYER
    #define _PLAYER
    
    #include <iomanip>		// use for setting field width
    #include <time.h>		// use for generating random factor
    #include "container.h"
    
    enum job {sw, ar, mg};	/* define 3 jobs by enumerate type
    							   sword man, archer, mage */
    class player
    {
    	friend void showinfo(player &p1, player &p2);
    	friend class swordsman;
    
    protected:
    	int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV;
    	// General properties of all characters
    	string name;	// character name
    	job role;		/* character's job, one of swordman, archer and mage,
    					   as defined by the enumerate type */
    	container bag;	// character's inventory
    
    public:
    	virtual bool attack(player &p)=0;	// normal attack
    	virtual bool specialatt(player &p)=0;	//special attack
    	virtual void isLevelUp()=0;			// level up judgement
    
    	void reFill();		// character's HP and MP resume
    	bool death();		// report whether character is dead
    	void isDead();		// check whether character is dead
    	bool useHeal();		// consume heal, irrelevant to job
    	bool useMW();		// consume magic water, irrelevant to job
    	void transfer(player &p);	// possess opponent's items after victory
    	void showRole();	// display character's job
    	
    private:
    	bool playerdeath;			// whether character is dead, doesn't need to be accessed or inherited
    };
    
    #endif
    They seem to be correct. Am I missing something here?

    EDIT: Error in the includes, should have included the right headers.
    Last edited by 843; 04-17-2011 at 06:50 AM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You NEED to look at the linker command and verify it is right.
    Remember the order the object files are in matter to some linkers.

    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HJ: unresolved symbols
    By abhyudaya in forum C Programming
    Replies: 4
    Last Post: 04-17-2011, 03:15 AM
  2. unresolved external symbols...linking errors in VC++
    By rammohan2b in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2009, 02:19 AM
  3. Unresolved External
    By ElWhapo in forum Windows Programming
    Replies: 1
    Last Post: 05-08-2005, 08:21 PM
  4. unresolved external...
    By matheo917 in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2002, 11:19 PM
  5. Unresolved external symbols in OGL
    By Shadow12345 in forum Game Programming
    Replies: 4
    Last Post: 08-04-2002, 09:46 PM