Undefined reference errorsto function declared in header file but defined in Cpp file

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 2
Reputation: quophyie is an unknown quantity at this point 
Solved Threads: 0
quophyie quophyie is offline Offline
Newbie Poster

Undefined reference errorsto function declared in header file but defined in Cpp file

 
0
  #1
Mar 24th, 2008
Can one of you gurus give me some help please. I am a new programmer so please bear with me. 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.

Deck.h header file
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. namespace pack{
  9. struct card{
  10.  
  11. // this holds the description of the card e.g. "King Of"
  12. std::string card_name;
  13.  
  14. //card type e.g. "Hearts", Spades, etc
  15. std::string card_type;
  16. /*
  17. * the value of the card e.g, 2,3 6 etc Note that Jack = 10
  18. * Queen = 11, King = 12, Ace = 13
  19. */
  20. int card_value;
  21. };
  22.  
  23. }
  24. class Deck
  25.  
  26. {
  27. pack::card deck;
  28.  
  29. public:
  30. Deck();
  31. ~Deck();
  32.  
  33. // returns a card
  34. pack::card getCard();
  35.  
  36. // create a card
  37. inline void createCard(string card_name, string card_type, int card_value);
  38.  
  39. //retutrn a pack of cards as a vector
  40. vector <pack::card>getDeckOfcards();
  41.  
  42. //reset a card variable
  43. void resetCard(pack::card card);
  44.  
  45. //create a pack of cards
  46.  
  47. vector <pack::card>createPack();
  48.  
  49. };




Deck.cpp file
  1. #include "Deck.h"
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5. using namespace pack;
  6.  
  7.  
  8.  
  9.  
  10. /*defines a deck with a return type of deck i.e. Note that
  11. * the return type deck is from the namespace pack which is declared and defined in
  12. * the the header file "Deck.h"
  13. */
  14.  
  15. extern pack::card deck;
  16.  
  17.  
  18. /*defines a getCard with a return type of deck i.e. Note that
  19.  * the return type deck is from the namespace pack which is declared and defined in
  20.  * the the header file "Cards.h"
  21.  */
  22. pack::card getCard();
  23.  
  24. //vector holding a deck of cards
  25. vector <pack::card>deckOfCards;
  26.  
  27. //
  28. inline void Deck::createCard(string card_name, string card_type, int card_value)
  29. {
  30. deck.card_name=card_name;
  31. deck.card_type = card_type;
  32. deck.card_value= card_value;
  33. };
  34.  
  35. //return deck of cards
  36. //vector <pack::card>getDeckOfcards();
  37.  
  38. //reset a card variable
  39. //void resetCard(pack::card card);
  40.  
  41. vector <pack::card>Deck::createPack(){
  42.  
  43. int type = 0;
  44. int value = 2;
  45. string card_name, card_type;
  46. int card_value;
  47.  
  48. switch(type){
  49. case 0:
  50. card_type = "Heart";
  51. break;
  52. case 1:
  53. card_type = "Spade";
  54. break;
  55.  
  56. case 2:
  57. card_type = "Clubs";
  58. break;
  59.  
  60. case 3:
  61. card_type = "Diamond";
  62. break;
  63. }
  64.  
  65. switch(value){
  66.  
  67. case 2:
  68. card_name = "2 of ";
  69. break;
  70.  
  71. case 3:
  72. card_name = "3 of ";
  73. break;
  74.  
  75. case 4:
  76. card_name = "4 of ";
  77. break;
  78.  
  79. case 5:
  80. card_name = "5 of ";
  81. break;
  82.  
  83. case 6:
  84. card_name = "6 of ";
  85. break;
  86.  
  87. case 7:
  88. card_name = "7 of ";
  89. break;
  90.  
  91. case 8:
  92. card_name = "8 of ";
  93. break;
  94.  
  95. case 9:
  96. card_name = "9 of ";
  97. break;
  98.  
  99. case 10:
  100. card_name = "10 of ";
  101. break;
  102.  
  103. case 11:
  104. card_name = "Jack of ";
  105. break;
  106.  
  107. case 12:
  108. card_name = "Queen of ";
  109. break;
  110.  
  111. case 13:
  112. card_name = "King of ";
  113. break;
  114.  
  115. case 14:
  116. card_name = "Ace of ";
  117. break;
  118. }
  119. vector <pack::card>::iterator iter;
  120. for (;type<4;type++){
  121. for (;value<15;value++){
  122. deck.card_name = card_name;
  123. deck.card_type = card_type;
  124. deck.card_value = value;
  125.  
  126. deckOfCards.push_back(deck);
  127. resetCard(deck);
  128.  
  129. }
  130. value = 2;
  131. }
  132. return deckOfCards;
  133. }
  134.  
  135.  
  136. Deck::Deck()
  137. {
  138.  
  139.  
  140. }
  141.  
  142. Deck::~Deck()
  143. {
  144. }
  145.  
  146. pack::card Deck::getCard(){
  147.  
  148. return deck;
  149. }
  150.  
  151. vector <pack::card> Deck::getDeckOfcards(){
  152. return deckOfCards;
  153. }
  154.  
  155.  
  156.  
  157. void Deck::resetCard(pack::card card){
  158. card.card_name="";
  159. card.card_type= "";
  160. card.card_value = 0;
  161. }





PokerEngine.cpp file
  1. #include <string.h>
  2. #include "Player.h"
  3. #include "Deck.h"
  4. #include <iostream>
  5. using namespace std;
  6. using namespace pack;
  7.  
  8.  
  9. class Poker{
  10.  
  11. };
  12.  
  13. int main(){
  14.  
  15.  
  16. string pl_name = "Player1";
  17. int pl_id = 0;
  18. Deck *dk; = new Deck;
  19.  
  20. dk->createPack();
  21. Player *pl = new Player(pl_name, pl_id);
  22. dk=0;
  23. delete dk;
  24.  
  25.  
  26.  
  27. return 0;
  28. }







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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Undefined reference errorsto function declared in header file but defined in Cpp

 
0
  #2
Mar 24th, 2008
Change
Deck *dk; = new Deck; in the main() function
To
Deck *dk = new Deck;
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 2
Reputation: quophyie is an unknown quantity at this point 
Solved Threads: 0
quophyie quophyie is offline Offline
Newbie Poster

Re: Undefined reference errorsto function declared in header file but defined in Cpp file

 
0
  #3
Mar 24th, 2008
Thanks. that was a typo. Anyway, I've resolved the problem. It was a linker error. I just updated my compiler i.e. mingw 3.4.5 to the latest of 5.1.3 and it works now. Thanks for your help anyway.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC