🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

gcc & templates... ugh...

Started by
1 comment, last by A. Buza 23 years, 2 months ago
I have a pretty simple set of template classes, but gcc/c++/etc refuse to compile them, giving very odd errors. I checked the gcc docs and known bugs list, but found nothing. Are there any ''gotchas'' when it comes to gpp that I haven''t heard of? This is starting to get irritating... I suppose someone will ask for my code, so here it is. I wouldn''t be surprised if the implementation is wrong. It first time writing a queue, and it was done at 3:00 am...ugh.. so tired... I actualy wrote a stack on accident on my first attempt. I had also been away from C++ for about a year. Ugh...
  
/*queue.hpp: You know what it is supposed to do. Not sure if it does it or
  not though... */

template <class T>
class queue_node
{
 private:
  T *data;
  queue_node *next;
};

template <class T>
class queue
{
 public:
  void add(T *data);
  T* get(void);
 private:
  queue_node *head;
  queue_node *tail;
};

template <typename T>
void  queue::add(T *data)
{
  queue_node *tmp_node;
  tmp_node = new stack_node;
  
  tmp_node->data = data;
  tail->next=tmp_node;
  tail=tmp_node;
};

template <typename T>
T* queue::get(void)
{
  queue_node *temp_node;
  T          *temp_data;
  
  temp_node = head;
  top=top->next;
  
  temp_data = temp_node->data;
  
  delete temp_node;
  return temp_data;
};
  
.. I just realized I forgot [con/de]structors.
Advertisement
Okay. What you are forgetting syntax-wise is that you need to add < T > to all templated types, including the ones in your function declaration and definition prototypes.

Here''s code that compiles with the RedHat 7.0 gcc compiler:

         2 /*queue.hpp:You know what it is supposed to do. Not sure if it does it or      3 notthough... */      4       5 template <class T>      6 class queue_node      7 {      8  private:      9   T *data;	 //The following compiled without the <T>     10   queue_node<T> *next;     11 };     12      13 template <class T>     14 class queue     15 {     16  public:     17      18   void add(T *data);     19   T* get(void);     20  private:     21   queue_node<T> *head;     22   queue_node<T> *tail;     23 };     24 template <typename T>     25 void  queue<T>::add(T *data)     26 {     27   queue_node<T> *tmp_node;     28   tmp_node = new queue_node<T>;     29      30   tmp_node->data = data;     31   tail->next=tmp_node;     32   tail=tmp_node;     33 };     34 template <typename T>     35 T* queue<T>::get(void)     36 {     37   queue_node<T> *temp_node;     38   T          *temp_data;     39      40   temp_node = head;     41   top=top->next;     42      43   temp_data = temp_node->data;     44      45   delete temp_node;     46   return temp_data;     47 };     48      49 int main(int argc, char **argv)     50 {     51      52 }  




joeG

joeG
Aha! That makes perfect sense. Thanks!

This topic is closed to new replies.

Advertisement