Sign in to follow this  
quophyie

undefined reference error to prototypes in header files which are defined in cpp file

Recommended Posts

quophyie    122
Can one of you gurus give me some help please. I have a header file called Deck.h and a cpp file called Deck.cpp. In my header file, I have a class called Deck which has prototype function called createPack as one of its member functions. the createPack function is defined in Deck.cpp. I also have a file called PokerEngine.cpp which holds my main function. However, when I create an instance of class Deck and try and call the function createPack, I get undefined reference errors. I have tried using both the "." and "->" operators and I am still getting the errors. I was hoping you guys will be able to help me solve this. I am using mingw 3.4.5 Deck.h header file

#include <iostream>
#include <string>
#include <vector>

using namespace std;


namespace pack{
	 struct card{

	 		// this holds the description of the card e.g. "King Of"
	 		std::string card_name;
	 		
	 		//card type e.g. "Hearts", Spades, etc
	 		std::string card_type;
	 		/*
	 		 * the value of the card e.g, 2,3 6 etc Note that Jack = 10
	 		 * Queen = 11, King = 12, Ace = 13
	 		 */
	 		int card_value;
	 		};
	 		
}
class Deck

{
	pack::card deck;
	
public:
	Deck();
	 ~Deck();
	 
		// returns a card
	pack::card getCard();

       // create a card
	inline void createCard(string card_name, string card_type, int card_value);
	
	//retutrn a pack of cards as a vector
	vector  <pack::card>getDeckOfcards();
	
	//reset a card variable
	void resetCard(pack::card card);
	
	//create a pack of cards
	
	vector <pack::card>createPack();

	};
	




Deck.cpp file

#include "Deck.h"
#include <iostream>
#include <vector>
using namespace std;
using namespace pack;



	
	/*defines a deck with a return type of deck i.e. Note that 
	 * the return type deck is from the namespace pack which is declared and defined in
	 * the the header file "Deck.h"
	 */ 
   
 extern pack::card deck;
    

/*defines a getCard with a return type of deck i.e. Note that 
 * the return type deck is from the namespace pack which is declared and defined in
 * the the header file "Cards.h"
 */ 
pack::card getCard();

//vector holding a deck of cards
	vector <pack::card>deckOfCards;

// 
inline void Deck::createCard(string card_name, string card_type, int card_value)
{
	deck.card_name=card_name; 
		deck.card_type = card_type; 
		deck.card_value= card_value;
};

//return deck of cards
//vector  <pack::card>getDeckOfcards();

//reset a card  variable
//void resetCard(pack::card card);

vector <pack::card>Deck::createPack(){
	
	int type = 0;
	int value = 2;
	string card_name, card_type; 
	int card_value;
	
switch(type){
case 0:
	card_type = "Heart";
	break;
case 1:
	card_type = "Spade";
	break;

case 2:
	card_type = "Clubs";
	break;

case 3:
	card_type = "Diamond";
	break;
}

switch(value){

case 2: 
card_name = "2 of ";
break;

case 3: 
card_name = "3 of ";
break;

case 4: 
card_name = "4 of ";
break;

case 5: 
card_name = "5 of ";
break;

case 6: 
card_name = "6 of ";
break;

case 7: 
card_name = "7 of ";
break;

case 8: 
card_name = "8 of ";
break;

case 9: 
card_name = "9 of ";
break;

case 10: 
card_name = "10 of ";
break;

case 11: 
card_name = "Jack of ";
break;

case 12: 
card_name = "Queen of ";
break;

case 13: 
card_name = "King of ";
break;

case 14:
card_name = "Ace of ";
break;
}
	vector <pack::card>::iterator iter;
 for (;type<4;type++){
	 for (;value<15;value++){
		deck.card_name = card_name;
		deck.card_type = card_type;
		deck.card_value = value;
		
		 deckOfCards.push_back(deck);
		 resetCard(deck);
		 
 }
	 value = 2;
}
 return deckOfCards;
}


	
	


Deck::Deck()
{
	
	
}

Deck::~Deck()
{
}

pack::card Deck::getCard(){
	
	return deck;
}

vector <pack::card> Deck::getDeckOfcards(){
	return deckOfCards;
}



void Deck::resetCard(pack::card card){
	card.card_name="";
	card.card_type= "";
	card.card_value = 0;
}




PokerEngine.cpp file
#include <string.h>
#include "Player.h"
#include "Deck.h"
#include <iostream>
using namespace std;
using namespace pack;


class Poker{
	
};

int main(){
	

	string pl_name = "Player1";
	int pl_id = 0;
	Deck *dk;
	
	dk->createPack();
	Player *pl = new Player(pl_name, pl_id);	

    
   
	
	return 0;
}





Compiler Errors **** Build of configuration Debug for project Poker **** **** Internal Builder is used for build **** g++ -O0 -g3 -Wall -c -fmessage-length=0 -oPokerGameEngine.o ..\PokerGameEngine.cpp ..\PokerGameEngine.cpp: In function `int main()': ..\PokerGameEngine.cpp:21: warning: unused variable 'pl' g++ -O0 -g3 -Wall -c -fmessage-length=0 -oDeck.o ..\Deck.cpp ..\Deck.cpp: In member function `std::vector<pack::card, std::allocator<pack::card> > Deck::createPack()': ..\Deck.cpp:48: warning: unused variable 'card_value' g++ -oPoker.exe lottery.o PokerGameEngine.o Player.o Deck.o PokerGameEngine.o: In function `main': D:/C_Files_02.03.2007/MyOwnDevelopedPrograms/Poker/Debug/../PokerGameEngine.cpp:20: undefined reference to `Deck::createPack()' collect2: ld returned 1 exit status Build error occurred, build is stopped Time consumed: 4250 ms. [Edited by - quophyie on March 24, 2008 9:43:44 AM]

Share this post


Link to post
Share on other sites
MJP    19754
I'm no really sure why you're getting a undefined reference error here, but you have another big problem:


Deck *dk;
dk->createPack();


You can't call member functions on a pointer to nothing. If what you want here is a static function, make it a static function and not a member function.

Share this post


Link to post
Share on other sites
quophyie    122
The code is exactly as posted. Excuse the whitesapce formatting. I have added the include guards but i'm still getting the error. I need to know why. I am using mingw 3.4.5

Share this post


Link to post
Share on other sites
Oluseyi    2103
It's a linker error. For whatever reason, the reference to Deck::createPack in PokerEngine.o is not resolving to the definition in Deck.o. What's your directory structure? What's the command line you issue? If you're not doing this via command line, what IDE are you using (and, please God, don't say you're using Dev-C++...)?

By the way, you want to include string, not string.h.

Share this post


Link to post
Share on other sites
quophyie    122
Thanks guys. I've resolved the problem. Like you guys suggested, it was a linker problem. I updated the compiler i.e. mingw from version 3.4.5 to the latest version of 5.1.3 and it works now. Hooray!!. Thanks for your help. By the way, my IDE is eclipse cdt 3.3

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this