Thread: Problem with templates

  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,793
    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, 06:24 PM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM