error when returning template type~

This is a discussion on error when returning template type~ within the C++ Programming forums, part of the General Programming Boards category; hi all~ i met some problem when coding a template class, the trouble is one of its method(const method) due ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    error when returning template type~

    hi all~

    i met some problem when coding a template class, the trouble is one of its method(const method) due to grab the data with position specified which return the type the same as template one, but when i compiled the compiler report an error which said: "warning: argument to non-pointer type 'int' from NULL". i wonder what this means and want the solution to my method, here is the code:
    Code:
    template <class T>
    class LinkedList
    {
    	public:
    		LinkedList();
    		~LinkedList();
    		void clear();
    		bool isEmpty() const;
    		int getLength() const;
    		T getData(int pos) const;
    		int find(T t) const;
    		bool insert(int pos, T t);
    		bool remove(int pos);
    		void trace() const;
    	private:
    		LinkedListNode<T>* __first;
    };
    template <class T>
    T LinkedList<T>::getData(int pos) const
    {
    	if (pos < 0) return NULL;
    	int _len = 0;
    	LinkedListNode<T>* _ptNode = __first;
    	while (_ptNode)
    	{
    		if (_len == pos) return _ptNode->data;
    		_ptNode = _ptNode->next;
    		_len++;
    	}
    	return NULL;
    }
    
    
    int main()
    {
    	LinkedList<int> myLinkedList;
    	int num = 10;
    	for (int i=0; i<num; i++)
    	{
    		myLinkedList.insert(i, i);
    	}
    	myLinkedList.remove(9);
    	myLinkedList.getData(1);                   // Error here !
    	myLinkedList.trace();
    	return 0;
    }
    thanx !
    Last edited by black; 06-01-2004 at 01:08 AM.
    Never end on learning~

  2. #2
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Simply return 0 instead of NULL. As the compiler tells you, the return value is not a pointer, but an int.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Quote Originally Posted by Carlos
    Simply return 0 instead of NULL. As the compiler tells you, the return value is not a pointer, but an int.
    ie to say, NULL is indeed a pointer itself ??? if then it will points to what if i set something like: int x = NULL ?
    Never end on learning~

  4. #4
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Quote Originally Posted by black
    ie to say, NULL is indeed a pointer itself ??? if then it will points to what if i set something like: int x = NULL ?
    It depends. Check your compiler's definition for NULL.
    In case of VC++ 6.0 it's:
    Code:
    /* Define NULL pointer value */
    
    #ifndef NULL
    #ifdef __cplusplus
    #define NULL    0
    #else
    #define NULL    ((void *)0)
    #endif
    #endif
    In this case if you're compiling C++ code (and as you're using templates it should be ) there should be no problem, I guess.
    However, there's no reason to prefer NULL above 0 for built-in types.

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    it works ! thanx man !!! code fixed are like this:
    Code:
    T LinkedList<T>::getData(int pos) const
    {
    	if (pos < 0) return 0;
    	int _len = 0;
    	LinkedListNode<T>* _ptNode = __first;
    	while (_ptNode)
    	{
    		if (_len == pos) return _ptNode->data;
    		_ptNode = _ptNode->next;
    		_len++;
    	}
    	return 0;
    }
    but, it is strange that compiler permit a number return say, 0 to pass, and if the T type are applied into some other data type like another class, will it be error again when return a 0 ?
    Never end on learning~

  6. #6
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Normally the conversion should be legal only if you declare a one-parameter constructor which performs implicit conversions. In this case, the parameter should be of type int.
    Code:
    class ClassA
    {
    public:
    ClassA();
    ClassA( int i_in ); // allows implicit conversion - unless declared explicit
    };
    Implicit conversion are not always welcome, as such conversions are uncontrollable and therefore unwanted. Declaring the constructor with the keyword explicit prohibits this behaviour.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 05:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

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