Problem with templates

This is a discussion on Problem with templates within the C++ Programming forums, part of the General Programming Boards category; hi all, i have the following code: PHP Code: #include <iostream.h> typedef struct node  *  ptr ; template  <class  T >  struct node { ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    26

    Angry Problem with templates

    hi all,
    i have the following code:

    PHP Code:
    #include <iostream.h>
    typedef struct node ptr;

    template <class Tstruct node{
    ptr next;
    T item;
    };

    int main()
    {
    // do nothing
    system("PAUSE");
    return 
    0;

    when i try to run it i get this error:
    [C++ Error] stack.cpp(4): E2356 Type mismatch in redeclaration of 'node'

    i think its from the typedef but i dunno how to fix it!
    thanks for any help.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    As your typedef will likely be local to the node....keep it local
    Code:
    #include <iostream.h>
    
    
    template<class T>
    struct node{
    
    typedef struct node* ptr;
    
    ptr next;
    
    T item;
    
    };
    
    
    
    int main()
    
    {
    
    // do nothing
    
    system("PAUSE");
    
    return 0;
    
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    26
    No its not local, im gonna use it inside another class which i haven't written yet!
    Any ideas?!

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You can use typedefs inside classes:

    node<T>::ptr
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2005, 11:22 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21