473,396 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Error on template functions returning pointers

8
I'm trying to template a pointer-based list class and several functions (the ones in which I try to return a pointer) are producing error messages.

The class:

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. class List : public BasicADT
  3. {
  4.     public:
  5.  
  6.         List();     // constructor
  7.         List(const List<T>& aList);    // copy constructor
  8.         virtual ~List();    // destructor
  9.  
  10.         // overloaded assignment operator
  11.         List<T> & operator=(const List<T>& rhs);
  12.  
  13.         // member functions
  14.         virtual bool isEmpty() const;
  15.         virtual int getLength() const;
  16.         virtual void insert(int index, const T& newItem);
  17.         virtual void remove(int index);
  18.         virtual void retrieve(int index, T& dataItem) const;
  19.         virtual void removeAll();
  20.  
  21.     private: 
  22.  
  23.         struct ListNode
  24.         {
  25.             T item;
  26.             ListNode *next;
  27.         };    // end ListNode
  28.  
  29.         int size;
  30.         ListNode *head;
  31.         ListNode *find(int index) const;
  32.         void copyListNodes(List<T> origList);
  33.  
  34.     protected:
  35.  
  36.         void setSize(int newSize);
  37.         ListNode *getHead() const;
  38.         void setHead(ListNode *ptr) const;
  39.         T getNodeItem(ListNode *ptr) const;
  40.         ListNode *getNextNode(ListNode *ptr) const;
  41.  
  42. } ;    // end List
The functions:

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. ListNode *List<T>::find(int index) const
  3. {
  4.     if ( (index < 1) || (index > getLength()) )
  5.     {
  6.         return null;
  7.     }
  8.  
  9.     else
  10.     {
  11.         ListNode *cur = head;
  12.  
  13.         for (int skip = 1; skip < index; ++skip)
  14.         {
  15.             cur = cur->next;
  16.         }    // end for
  17.  
  18.         return cur;
  19.     }    // end if    
  20. }    // end find
  21.  
  22. template <typename T>
  23. ListNode *List<T>::getHead() const
  24. {
  25.     return head;
  26.  
  27. }    // end getHead
  28.  
  29. template <typename T>
  30. ListNode *List<T>::getNextNode(ListNode *ptr) const
  31. {
  32.     return ptr->next;
  33. }    // end getNextNode
I'm getting the same error on the first line of each of the above functions:

Expand|Select|Wrap|Line Numbers
  1. error: expected constructor, destructor, or type conversion before '*' token
I've written constructors and destructors for the templated List class, so I'm not sure why only these three functions are giving me problems. The majority of my code for this project was copied out of the textbook, as per the assignment, so I'm not sure what the problem is. All of the other member functions seem to compile just fine.

Any help would be greatly appreciated!
Dec 2 '07 #1
13 3380
Laharl
849 Expert 512MB
Try moving the * to right after ListNode. So, ListNode* List<T>::getHead(), rather than ListNode *List<T>::getHead(). The second one implies that the class is called *List<T> and the function returns a ListNode. The first tells the compiler that the class is called List<T> and the function returns a ListNode pointer.
Dec 2 '07 #2
edyam
8
Hmm... Thanks for the suggestion, but it didn't seem to make a difference. Any other ideas?

Thanks again!
Dec 2 '07 #3
Ganon11
3,652 Expert 2GB
Try moving the * to right after ListNode. So, ListNode* List<T>::getHead(), rather than ListNode *List<T>::getHead(). The second one implies that the class is called *List<T> and the function returns a ListNode. The first tells the compiler that the class is called List<T> and the function returns a ListNode pointer.
Laharl, actually, the whitespace here is a matter of preference - especially since classnames cannot start with the * character (else every time you said int *x, you would get an error).

I expect that, since ListNode is itself a template struct, you need to treat it as a template in your return type:

Expand|Select|Wrap|Line Numbers
  1. ListNode<T>* List<T>::blahblahblah()
Dec 2 '07 #4
edyam
8
I expect that, since ListNode is itself a template struct, you need to treat it as a template in your return type:

Expand|Select|Wrap|Line Numbers
  1. ListNode<T>* List<T>::blahblahblah()
Thanks, Ganon, but all that does is produce the following error...

error: expected constructor, destructor, or type conversion before '<' token

Thanks again!
Dec 2 '07 #5
Actually, ListNode is a nontemplate, nested struct. Your function should return a List<T>::ListNode*. So

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. List<T>::ListNode* List<T>::foo();
  3.  
when declaring member functions inside classes, however, the List<T>:: is unnecessary.
Dec 2 '07 #6
edyam
8
Actually, ListNode is a nontemplate, nested struct. Your function should return a List<T>::ListNode*. So

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. List<T>::ListNode* List<T>::foo();
  3.  
when declaring member functions inside classes, however, the List<T>:: is unnecessary.
That makes a lot of sense, tavianator, but I'm still getting the same error. Thanks anyway.
Dec 2 '07 #7
edyam
8
I tried commenting out the three functions that were giving me errors, but that just caused me to get the following error on the first line of every member function:

error: expected initializer before '<' token

Any ideas/suggestions? Thanks in advance!
Dec 2 '07 #8
Ganon11
3,652 Expert 2GB
Wait, I just remembered;

Are you defining these functions in the same file as your class? Template functions have to be defined in the same place they are declared (i.e. in the .h file).
Dec 2 '07 #9
Actually, List<T>::ListNode is a dependent name, so typename is required. Declare the functions like this:

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. typename List<T>::ListNode* List<T>::find()
  3.  
etc.
Dec 2 '07 #10
edyam
8
Re: Ganon

Adding 'typename' before the function results in the following error:
error: expected nested-name-specifier before 'List'

Re: tavianator

They're in two seperate files, but I've included

#include "ListTemplate.cpp"

at the bottom of ListTemplate.h, as per my professor's instructions for the project. This should be equivalent to having them in the same file, correct?
Dec 2 '07 #11
I'm pretty sure you switched "re: Ganon" and "re: tavianator".

Anyway, the following code compiles fine with g++ 4.2.2. Some old compilers handle typename incorrectly, and that may be why the error persisted when you put typename List<T>::ListNode *List<T>::find(). Make sure you write it that way in all cases.

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. class List
  3. {
  4.   public:
  5.  
  6.     List();  // constructor
  7.     List(const List<T>& aList);// copy constructor
  8.     virtual ~List();  // destructor
  9.  
  10.     // overloaded assignment operator
  11.     List<T> & operator=(const List<T>& rhs);
  12.  
  13.     // member functions
  14.     virtual bool isEmpty() const;
  15.     virtual int getLength() const;
  16.     virtual void insert(int index, const T& newItem);
  17.     virtual void remove(int index);
  18.     virtual void retrieve(int index, T& dataItem) const;
  19.     virtual void removeAll();
  20.  
  21.   private: 
  22.  
  23.     struct ListNode
  24.     {
  25.       T item;
  26.       ListNode *next;
  27.     }; // end ListNode
  28.  
  29.     int size;
  30.     ListNode *head;
  31.     ListNode *find(int index) const;
  32.     void copyListNodes(List<T> origList);
  33.  
  34.   protected:
  35.  
  36.     void setSize(int newSize);
  37.     ListNode *getHead() const;
  38.     void setHead(ListNode *ptr) const;
  39.     T getNodeItem(ListNode *ptr) const;
  40.     ListNode *getNextNode(ListNode *ptr) const;
  41.  
  42. } ;// end List
  43.  
  44. template <typename T>
  45. typename List<T>::ListNode *List<T>::find(int index) const
  46. {
  47.   if ( (index < 1) || (index > getLength()) )
  48.   {
  49.     return 0;
  50.   }
  51.  
  52.   else
  53.   {
  54.     ListNode *cur = head;
  55.  
  56.     for (int skip = 1; skip < index; ++skip)
  57.     {
  58.       cur = cur->next;
  59.     } // end for
  60.  
  61.     return cur;
  62.   } // end if 
  63. } // end find
  64.  
  65. template <typename T>
  66. typename List<T>::ListNode *List<T>::getHead() const
  67. {
  68.   return head;
  69.  
  70. } // end getHead
  71.  
  72. template <typename T>
  73. typename List<T>::ListNode *List<T>::getNextNode(ListNode *ptr) const
  74. {
  75.   return ptr->next;
  76. } // end getNextNode
  77.  
Dec 2 '07 #12
Ganon11
3,652 Expert 2GB
Re: tavianator

They're in two seperate files, but I've included

#include "ListTemplate.cpp"

at the bottom of ListTemplate.h, as per my professor's instructions for the project. This should be equivalent to having them in the same file, correct?
Yeah, this should have been Re:Ganon, but I figured it out XD

No, this is not the same. #include "ListTemplate.cpp" just means your program knows about the existence of the .cpp file and will look there for the function definitions. You need to define template functions in the same file i.e. in ListTemplate.h. You can still have the other functions in ListTemplate.cpp.
Dec 2 '07 #13
edyam
8
Sorry about the mixup! lol. Both of you have been extremely helpful. Using both of your suggestions, I'm no longer getting the original error messages, but the whole project still wont compile correctly. I intend to work on it some more later this evening, and I'll be sure to let you know about any new error messages I may get.

Thanks again for all your help.
Dec 2 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Phil | last post by:
Ok, I have a template function for any pointer to type T: template <typename T> void func(T* p) { DoSomethingGeneric(p); } Can I specialize this template for pointers to functions (or...
3
by: Filipe Valepereiro | last post by:
I all. I need to write a function that convert one string into a vector. This string represent a serialized form of the vector. So i come up with this piece of code, that compile just fine....
12
by: Sydex | last post by:
When I compile code I get error C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(double)' in this part : double Integration::quad2d(double...
1
by: DiskMan | last post by:
System: Redhat 7.2 Kernel-2.6.11.8 GCC-3.4.3 CCC-6.5.9 Binutils-2.15 Make-3.80 GTK/GLIB-2.6.7 For some reason my Linux box is suddenly having issues trying to read ;
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
7
by: wonderboy | last post by:
Hey guys, I have a simple question. Suppose we have the following functions:- //-----My code starts here char* f1(char* s) { char* temp="Hi"; return temp;
2
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode:...
1
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.