Templates. Help!

This is a discussion on Templates. Help! within the C++ Programming forums, part of the General Programming Boards category; I have the following code: Code: template <class T> class List { public: List *urm; T *info; List(); ~List(){ delete(this->urm); ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    19

    Templates. Help!

    I have the following code:

    Code:
    template <class T> class List
    {
    public:
    	List *urm;
    	T *info;
    	List();
    	~List(){
    		delete(this->urm);
    	}
    	List<T>* Add(List* ,T*);
    };
    
    
    template <class T> List<T>::List()
    {
    	urm=NULL;
    	info=NULL;
    }
    
    template <class T> List<T>* List<T>::Add(List *head,T *elem)
    {
    	List *aux;
    	if (head==NULL){
    		head=new List<T>;
    		head->info=elem;
    	}
    	else {
    	  aux=new List<T>;
    	  aux->info=elem;
    	  aux->urm=head;
              head=aux;
    	}
    	return head;
    }
    int main ()
    {
        List<int> *h=NULL;
        int a=1;
        h=h->Add(h,&a);
    
        return 0;
    }
    I don't see why it is not compiling

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I don't see why it is not compiling
    What errors do you get?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You seem to be combining the concepts of a List and a Node, is there a reason you decided to do that?

    It is generally easier to get a container working with a specific datatype first, and then convert it to a template. Just as a general suggestion you might consider doing that instead.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    19
    Code:
    template <class T> List<T>* List<T>::Add(List *head,T *elem)
    The error I get: "Invalid use of template 'List' "

    When I use:

    Code:
    template <class T> List<T>* List<T>::Add(List<T> *head,T *elem)
    "Linker error: Undifined symbol list<int>::add(list<int>near*,int near*)"

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    19
    Quote Originally Posted by Daved View Post
    You seem to be combining the concepts of a List and a Node, is there a reason you decided to do that?

    It is generally easier to get a container working with a specific datatype first, and then convert it to a template. Just as a general suggestion you might consider doing that instead.
    I just wanna try things out. In this code I see a problem I don't understand and I wanna know why it is not working. I allready found a way how to solve this problem. If I don't use "List *head" my class List is working fine. But still i thing there is a way to use that variable.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Did you change List<T>* to List* in both the declaration and the definition of Add?
    Last edited by Daved; 01-10-2008 at 05:39 PM.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    19
    Quote Originally Posted by Daved View Post
    Did you change List<T>* to List* in both the declaration and the definition of Add?
    Sure !!! Stil not working the same error :-(

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It compiles for me either way (VC++ 7.1). Is that your complete code? Which compiler do you use?

    BTW, you are doing something illegal with h->Add, though because at that point h is null.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Is all the code in the header file, or did you split the implementation into a .cpp file?

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    19
    Quote Originally Posted by Daved View Post
    It compiles for me either way (VC++ 7.1). Is that your complete code? Which compiler do you use?

    BTW, you are doing something illegal with h->Add, though because at that point h is null.
    Hm ... curios in Visual C++ 2005 Express Edition it's compiling fine and working fine without any changes and in all ways prezented. In Borland C 3.1 It's not compiling. And under linux using gcc stil not compiling (

    I also tryed this:
    Code:
    int main ()
    {
        List<int> *h;
        int a=1;
        h=h->Add(h,&a);
    
        return 0;
    }
    I also have "#include <stdio.h>" and that's my entire code. I didn't split my code in different files. I just forgot to paste the part with "#include <stdio.h>"
    Last edited by hanniball; 01-10-2008 at 06:03 PM.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It also compiles fine under DevC++ (the compiler is mingw32-gcc-3.3.1).

    Borland C 3.1 I'm not surprised.

    Under linux did you type gcc or g++, because you should be typing g++.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I also tryed this
    That's not the part that isn't compiling, although once you get it to compile that is just as bad as the previous version because you are dereferencing an uninitialized pointer.

    Did you copy and paste the exact error message? The linker error you typed refers to list<int> which is different than List<int>.

  13. #13
    Registered User
    Join Date
    Jan 2007
    Posts
    19
    Quote Originally Posted by Daved View Post
    >> I also tryed this
    That's not the part that isn't compiling, although once you get it to compile that is just as bad as the previous version because you are dereferencing an uninitialized pointer.

    Did you copy and paste the exact error message? The linker error you typed refers to list<int> which is different than List<int>.
    I wrote the error message by hand because I can't copy paste an error message from Borland C 3.1. I check it severel times and I wrote it correctly. In Borland C 3.1 in all error messages the names of functions and classes are untercase.

    I solve the problem so:
    Code:
    int main ()
    {
        List<int> *h= new List<int>;
        int a=1;
        h=h->Add(h,&a);
    
        return 0;
    }

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I'm glad it's compiling for you now.

    Your code still isn't quite right. It is legal because you are no longer dereferencing an uninitialized pointer, but now you have a memory leak. Logically, it doesn't make sense, and it still stems from the fact that you are using your List class both as a container and as a Node.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 12:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2005, 11:22 PM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM

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