Template, code simple qui ne marche pas

Template, code simple qui ne marche pas - C++ - Programmation

Marsh Posté le 08-09-2003 à 10:18:51    

Mon but est de convertir ma class team queue en un template.
 
mais avant, j ai commence avec un bout de code  :
 
 
 
test.h

Code :
  1. #ifndef TEST_H
  2. #define TEST_H
  3. using namespace std;
  4. template <class T>
  5. class test
  6. {
  7.     public :
  8.    
  9.         void output(T item);
  10. };
  11. #endif


 
test.cpp

Code :
  1. #include "test.h"
  2. #include <iostream>
  3. using namespace std;
  4. template <class T>
  5. void test<T>::output(T item)
  6. {
  7.     cout << item ;
  8. }


 
main.cpp

Code :
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include "test.h"
  4. using namespace std;
  5. int main()
  6. {
  7.   test<int> mytest;
  8.   mytest.output(6);
  9.   system("PAUSE" );
  10.   return 0;
  11. }


 
 
[Linker error] undefined reference to `test<int>::output(int)'
 :cry:  
 
 
pour la class team queue les commentaires sont bienvenu
(je le sens mal sur ce coup  :whistle: )
 
team_queue.h

Code :
  1. #ifndef TEAM_QUEUE_H
  2. #define TEAM_QUEUE_H
  3. #include <vector>
  4. using namespace std;
  5. class team_queue
  6. {
  7.     public :
  8.        
  9.         struct teamq_struct
  10.         {
  11.                 vector <int> vect_item;
  12.                 int item_team;
  13.         };
  14.        
  15.         team_queue();
  16.         //default constructor
  17.        
  18.         ~team_queue();
  19.         //default destructor
  20.        
  21.         void push (int item,int team);
  22.         ///adds the supplied item belonging to the given team number
  23.         // to the team_queue in the apporpriate fashion
  24.      
  25.         void pop();
  26.         //removes the front from the team_queue
  27.    
  28.         void team_pop(int team);
  29.         //removes the front item of the supplied team from the team_queue
  30.  
  31.         void team_remove(int team);
  32.         //removes all items belonging to a suppliedteam from the team_queue
  33.  
  34.         int front();
  35.         //returns the item at the front of the team_queue
  36.      
  37.         int team_front(int team);
  38.         //returns the item at the front of the supplied team from the team_queue
  39.    
  40.         bool pure();
  41.         //returns true iff the team_queue contains no more than 1 member of each team.
  42.  
  43.         int size();
  44.         //returns the number of items in the team_queue.  
  45.    
  46.         int team_size(int team);
  47.         //returns the number of elements of a given team in the team_queue
  48.        
  49.         int team_number();
  50.         //returns the number of different teams in the team_queue
  51.        
  52.         void order_asc();
  53.         //sorts the items in each team in the team_queue into nondescending
  54.         //numeric order (from front to rear).
  55.        
  56.         void order_desc();
  57.         //sorts the items in each team in the team_queue into nondascending
  58.         //numeric order (from rear to front).
  59.        
  60.         void display();
  61.         //display all the elements of the team queue
  62.      
  63.         bool isempty();
  64.         //return true if the tq is empty
  65.          
  66.      private:
  67.    
  68.         vector <teamq_struct> struct_vect;
  69.         vector <teamq_struct>::iterator  cursor;
  70.         void error(int errorcode);
  71.    
  72. };
  73. #endif


 
 
team_queue.cpp

Code :
  1. #include "team_queue.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. /*
  6. |¯¯¯¯¯¯¯¯¯¯¯¯|
  7. constructor
  8. |____________|
  9. */
  10. team_queue::team_queue()
  11. }
  12. /*
  13. |¯¯¯¯¯¯¯¯¯¯¯¯|
  14. destructor
  15. |____________|
  16. */
  17. team_queue::~team_queue()
  18. {
  19.     struct_vect.clear();
  20. }
  21. /*
  22. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  23. PUSH : adds the supplied item belonging to the given team number
  24. to the team_queue in the apporpriate fashion.
  25. |________________________________________________________________|
  26. */
  27. void team_queue::push(int item, int team)
  28. {   
  29.      team_queue::teamq_struct var;
  30.      var.item_team = team;
  31.      var.vect_item.push_back(item);
  32.        
  33.      if (isempty()) struct_vect.push_back(var);
  34.      else
  35.      {
  36.    
  37.      bool inserted = false;
  38.                            
  39.      for (cursor = struct_vect.begin(); cursor != struct_vect.end(); cursor++)
  40.      {
  41.           if (cursor->item_team == team) 
  42.           {
  43.                     cursor->vect_item.push_back(item); 
  44.                     inserted = true;
  45.                     break;   
  46.           }
  47.      }
  48.      //if no team member found enqueue at the end
  49.      if (inserted == false) struct_vect.push_back(var);; 
  50.      }
  51. }
  52. /*
  53. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  54.   POP : removes the front from the team_queue
  55. |_______________________________________________|
  56. */
  57. void team_queue::pop()
  58. {
  59.      if (isempty()) error(0);
  60.      else
  61.      {
  62.           cursor = struct_vect.begin();   
  63.           if (cursor->vect_item.size() == 1) //only one element ?
  64.           {
  65.                    struct_vect.erase(cursor);  //destroy the full team (1elmt)
  66.           }       
  67.           else cursor->vect_item.erase(cursor->vect_item.begin());
  68.      }
  69. }
  70. /*
  71. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  72.   TEAM_POP : removes the front item of the supplied team from the team_queue
  73. |_____________________________________________________________________________|
  74. */
  75. void team_queue::team_pop(int team)
  76. {
  77. if (isempty()) error(0);
  78. else
  79. {
  80.     for (cursor = struct_vect.begin(); cursor != struct_vect.end(); cursor++)
  81.     {
  82.         if (cursor->item_team == team)
  83.         {
  84.                 if (cursor->vect_item.size() == 1) struct_vect.erase(cursor);
  85.                 else cursor->vect_item.erase(cursor->vect_item.begin()); 
  86.                 break;
  87.         }
  88.     }
  89. }
  90. }
  91. /*
  92. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  93. TEAM_REMOVE :removes all items belonging to a suppliedteam from the team_queue
  94. |______________________________________________________________________________|
  95. */
  96. void team_queue::team_remove(int team)
  97. {
  98. if (isempty()) error(0);
  99. else
  100. {
  101.     for (cursor = struct_vect.begin(); cursor != struct_vect.end(); cursor++)
  102.     {
  103.         if (cursor->item_team == team) 
  104.         {
  105.               struct_vect.erase(cursor); 
  106.               cursor --;         
  107.         }   
  108.     }
  109. }
  110. }
  111. /*
  112. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  113. FRONT :returns the item at the front of the team_queue
  114. |_________________________________________________________|
  115. */
  116. int team_queue::front()
  117. {
  118.     if (isempty())
  119.     {
  120.          error(1);
  121.          return 0;
  122.     }
  123.     else return struct_vect[0].vect_item.front();
  124. }
  125. /*
  126. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  127. TEAM _FRONT :returns the item at the front of the supplied team  
  128. from the team_queue
  129. |_________________________________________________________________|
  130. */
  131. int team_queue::team_front(int team)
  132. {
  133.      if (isempty()) error(1);
  134.      else
  135.      {
  136.           for (cursor = struct_vect.begin(); cursor != struct_vect.end(); cursor++)
  137.           {
  138.                     if (cursor->item_team == team)
  139.                     {
  140.                                         return cursor->vect_item.at(0);
  141.                                         break;
  142.                     }         
  143.           }
  144.           error(3);
  145.           return 0; //??  
  146.      }
  147. }
  148. /*
  149. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  150. PURE : returns true iff the team_queue contains no more  
  151. than 1 member of each team.
  152. |_________________________________________________________|
  153. */
  154. bool team_queue::pure()
  155. {
  156.     if (isempty()) return false;
  157.     else
  158.     {
  159.         for (cursor = struct_vect.begin(); cursor != struct_vect.end(); cursor++)
  160.         if (cursor->vect_item.size() != 1) return false;
  161.         return true;
  162.     }
  163. }
  164. /*
  165. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  166. SIZE : returns the number of items in the team_queue.  
  167. |_____________________________________________________|
  168. */
  169. int team_queue::size()
  170. {
  171.     if (isempty())  return 0;
  172.     else
  173.     {
  174.         int count = 0;
  175.         for (cursor = struct_vect.begin(); cursor != struct_vect.end(); cursor++)
  176.         count = count + (cursor->vect_item.size());   
  177.         return count;
  178.     }
  179. }
  180. /*
  181. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  182. TEAM_SIZE : returns the number of elements of a given team in the team_queue.  
  183. |_____________________________________________________________________________|
  184. */
  185. int team_queue::team_size(int team)
  186. {
  187.     if (isempty())  return 0; //if tq is empty return 0
  188.     else
  189.     {
  190.           for (cursor = struct_vect.begin(); cursor != struct_vect.end(); cursor++)
  191.           if (cursor->item_team == team)   return cursor->vect_item.size();
  192.      }
  193. }
  194. /*
  195. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  196. TEAM_NUMBER : returns the number of different teams in the team_queue.  
  197. |_______________________________________________________________________|
  198. */
  199. int team_queue::team_number() { return struct_vect.size(); }
  200. /*
  201. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  202. ORDER_ASC : sorts the items in each team in the team_queue into nondescending
  203. numeric order (from rear to front).  
  204. |_____________________________________________________________________________|
  205. */
  206. void  team_queue::order_asc()
  207. {
  208. if (isempty())  { return; }
  209. else
  210. {
  211.     for (cursor = struct_vect.begin(); cursor != struct_vect.end(); cursor++)
  212.     {
  213.         int length = cursor->vect_item.size();
  214.         sort (cursor->vect_item.begin(), cursor->vect_item.end());     
  215.     }     
  216. }//end else
  217. }
  218. /*
  219. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  220. ORDER_DESC : sorts the items in each team in the team_queue into nonascending
  221. numeric order (from front to rear).  
  222. |_____________________________________________________________________________|
  223. */
  224. void  team_queue::order_desc()
  225. {
  226. if (isempty())  { return; }
  227. else
  228. {
  229.     for (cursor = struct_vect.begin(); cursor != struct_vect.end(); cursor++)
  230.     {
  231.         int length = cursor->vect_item.size();
  232.         sort (cursor->vect_item.rbegin(), cursor->vect_item.rend());
  233.     }     
  234. }//end else
  235. }
  236. /*
  237. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  238. DISPLAY : display all the elements of the team queue
  239. |______________________________________________________|
  240. */
  241. void team_queue::display()
  242. {
  243.      if (isempty()) error(1);
  244.      else
  245.      {
  246.           for (cursor = struct_vect.begin(); cursor != struct_vect.end(); cursor++)
  247.           for (int i =0; i < cursor->vect_item.size();i++)  cout << " (" <<  cursor->vect_item[i] << "," <<  cursor->item_team << " ) " << flush;
  248.           cout << endl;
  249.      }
  250. /*
  251. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  252. ERROR :  
  253. |______________________________________________________|
  254. */
  255. void team_queue::error(int errorcode)
  256. {
  257. switch (errorcode)
  258. {
  259.     case 0 : cout << "Error, you can't pop anymore items \a" << endl; break;
  260.     case 1 : cout << "Teamq queue is empty \a" << endl; break;
  261.     case 3 : cout << "No team found \a" << endl; break;
  262. }
  263. }
  264. /*
  265. |¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
  266. ISEMPTY: return true id there is no element into the team_q
  267. |____________________________________________________________|
  268. */
  269. bool team_queue::isempty() { return (struct_vect.size() == 0); }

Reply

Marsh Posté le 08-09-2003 à 10:18:51   

Reply

Marsh Posté le 08-09-2003 à 10:33:31    

il faut que tout tes classes templlaaaaaatteeeee soiennnt dans la meeeemmmeee uniteeeeee de compilaaaatiooooooon
 
 
Aaaaaammmeeeennnnnnn

Reply

Marsh Posté le 08-09-2003 à 10:41:47    

chrisbk a écrit :

il faut que tout tes classes templlaaaaaatteeeee soiennnt dans la meeeemmmeee uniteeeeee de compilaaaatiooooooon
 
 
Aaaaaammmeeeennnnnnn


gni ?
exemple pleassse
 :(  :D

Reply

Marsh Posté le 08-09-2003 à 10:43:40    

xiluoc a écrit :


gni ?
exemple pleassse
 :(  :D  


 
le bouton rechercher du forum est la pour repondre a toutes vos question. Elegant, sobre, fiable, c'est le compagnon ideal et fidele qui sera toujours a vos coté en cas d'ennui. (editions del prado)

Reply

Marsh Posté le 08-09-2003 à 10:45:00    

chrisbk a écrit :


 
le bouton rechercher du forum est la pour repondre a toutes vos question. Elegant, sobre, fiable, c'est le compagnon ideal et fidele qui sera toujours a vos coté en cas d'ennui. (editions del prado)
 


 :kaola:

Reply

Marsh Posté le 08-09-2003 à 10:45:10    

Grossièrement remagné:
 
test.h
 

Code :
  1. #ifndef TEST_H
  2. #define TEST_H
  3.   #include <iostream>
  4.   template <class T>
  5.  
  6.   class test
  7.   {
  8.         public :
  9.        
  10.             void output(T item)
  11.              {
  12.                  using namespace std;
  13.                  cout << item ;
  14.              }
  15.   };
  16.        
  17.   #endif


 


---------------
Le Tyran
Reply

Marsh Posté le 08-09-2003 à 10:49:15    

test.h
 

Code :
  1. #ifndef TEST_H
  2. #define TEST_H
  3.   #include <iostream>
  4.   template <class T>
  5.  
  6.   class test
  7.   {
  8.         public :
  9.        
  10.             void output(T item)
  11.              {
  12.                  std::cout << item ;
  13.              }
  14.   };
  15.        
  16.   #endif


 
c mieux qd meme  :whistle:  
 
le truc c que ton code des fonctions/methodes tempaltes doivent etre dans le .h non pas dans un .cc a part.

Reply

Marsh Posté le 08-09-2003 à 10:52:52    

Joel F a écrit :

test.h
 

Code :
  1. #ifndef TEST_H
  2. #define TEST_H
  3.   #include <iostream>
  4.   template <class T>
  5.  
  6.   class test
  7.   {
  8.         public :
  9.        
  10.             void output(T item)
  11.              {
  12.                  std::cout << item ;
  13.              }
  14.   };
  15.        
  16.   #endif


 
c mieux qd meme  :whistle:  
 
le truc c que ton code des fonctions/methodes tempaltes doivent etre dans le .h non pas dans un .cc a part.


 
 :sarcastic: J'ai eu la flemme :D


---------------
Le Tyran
Reply

Marsh Posté le 08-09-2003 à 11:33:06    

Joel F a écrit :

test.h
 

Code :
  1. #ifndef TEST_H
  2. #define TEST_H
  3.   #include <iostream>
  4.   template <class T>
  5.  
  6.   class test
  7.   {
  8.         public :
  9.        
  10.             void output(T item)
  11.              {
  12.                  std::cout << item ;
  13.              }
  14.   };
  15.        
  16.   #endif


 
c mieux qd meme  :whistle:  
 
le truc c que ton code des fonctions/methodes tempaltes doivent etre dans le .h non pas dans un .cc a part.


 
haaaaaaa
ok thks  :hello:

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed