Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
7354 connectés 

  FORUM HardWare.fr
  Programmation
  C++

  Template, code simple qui ne marche pas

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Template, code simple qui ne marche pas

n°509979
xiluoc
un pc pour les unirs ....
Posté le 08-09-2003 à 10:18:51  profilanswer
 

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

mood
Publicité
Posté le 08-09-2003 à 10:18:51  profilanswer
 

n°509991
chrisbk
-
Posté le 08-09-2003 à 10:33:31  profilanswer
 

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

n°509996
xiluoc
un pc pour les unirs ....
Posté le 08-09-2003 à 10:41:47  profilanswer
 

chrisbk a écrit :

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


gni ?
exemple pleassse
 :(  :D

n°509998
chrisbk
-
Posté le 08-09-2003 à 10:43:40  profilanswer
 

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)

n°509999
xiluoc
un pc pour les unirs ....
Posté le 08-09-2003 à 10:45:00  profilanswer
 

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:

n°510000
LetoII
Le dormeur doit se réveiller
Posté le 08-09-2003 à 10:45:10  profilanswer
 

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
n°510002
Joel F
Real men use unique_ptr
Posté le 08-09-2003 à 10:49:15  profilanswer
 

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.

n°510004
LetoII
Le dormeur doit se réveiller
Posté le 08-09-2003 à 10:52:52  profilanswer
 

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
n°510042
xiluoc
un pc pour les unirs ....
Posté le 08-09-2003 à 11:33:06  profilanswer
 

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:


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  Template, code simple qui ne marche pas

 

Sujets relatifs
Instanciation d objets OLE ca ne marche que dans l unite principale ?onmouseover qui marche pas tout le temps [RESOLU]
fonction membres et template .templatebbcode ca marche pas
Votre bout de code le mieux faitMon code..
Ce site ne marche sous Camino 0.7déclaration d'un template
cacher code html ? 
Plus de sujets relatifs à : Template, code simple qui ne marche pas


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)