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: - template <typename T>
-
class List : public BasicADT
-
{
-
public:
-
-
List(); // constructor
-
List(const List<T>& aList); // copy constructor
-
virtual ~List(); // destructor
-
-
// overloaded assignment operator
-
List<T> & operator=(const List<T>& rhs);
-
-
// member functions
-
virtual bool isEmpty() const;
-
virtual int getLength() const;
-
virtual void insert(int index, const T& newItem);
-
virtual void remove(int index);
-
virtual void retrieve(int index, T& dataItem) const;
-
virtual void removeAll();
-
-
private:
-
-
struct ListNode
-
{
-
T item;
-
ListNode *next;
-
}; // end ListNode
-
-
int size;
-
ListNode *head;
-
ListNode *find(int index) const;
-
void copyListNodes(List<T> origList);
-
-
protected:
-
-
void setSize(int newSize);
-
ListNode *getHead() const;
-
void setHead(ListNode *ptr) const;
-
T getNodeItem(ListNode *ptr) const;
-
ListNode *getNextNode(ListNode *ptr) const;
-
-
} ; // end List
The functions: - template <typename T>
-
ListNode *List<T>::find(int index) const
-
{
-
if ( (index < 1) || (index > getLength()) )
-
{
-
return null;
-
}
-
-
else
-
{
-
ListNode *cur = head;
-
-
for (int skip = 1; skip < index; ++skip)
-
{
-
cur = cur->next;
-
} // end for
-
-
return cur;
-
} // end if
-
} // end find
-
-
template <typename T>
-
ListNode *List<T>::getHead() const
-
{
-
return head;
-
-
} // end getHead
-
-
template <typename T>
-
ListNode *List<T>::getNextNode(ListNode *ptr) const
-
{
-
return ptr->next;
-
} // end getNextNode
I'm getting the same error on the first line of each of the above functions: - 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!
13 3380
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.
Hmm... Thanks for the suggestion, but it didn't seem to make a difference. Any other ideas?
Thanks again!
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: - ListNode<T>* List<T>::blahblahblah()
I expect that, since ListNode is itself a template struct, you need to treat it as a template in your return type: - 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!
Actually, ListNode is a nontemplate, nested struct. Your function should return a List<T>::ListNode*. So -
template <typename T>
-
List<T>::ListNode* List<T>::foo();
-
when declaring member functions inside classes, however, the List<T>:: is unnecessary.
Actually, ListNode is a nontemplate, nested struct. Your function should return a List<T>::ListNode*. So -
template <typename T>
-
List<T>::ListNode* List<T>::foo();
-
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.
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!
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).
Actually, List<T>::ListNode is a dependent name, so typename is required. Declare the functions like this: -
template <typename T>
-
typename List<T>::ListNode* List<T>::find()
-
etc.
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?
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. -
template <typename T>
-
class List
-
{
-
public:
-
-
List(); // constructor
-
List(const List<T>& aList);// copy constructor
-
virtual ~List(); // destructor
-
-
// overloaded assignment operator
-
List<T> & operator=(const List<T>& rhs);
-
-
// member functions
-
virtual bool isEmpty() const;
-
virtual int getLength() const;
-
virtual void insert(int index, const T& newItem);
-
virtual void remove(int index);
-
virtual void retrieve(int index, T& dataItem) const;
-
virtual void removeAll();
-
-
private:
-
-
struct ListNode
-
{
-
T item;
-
ListNode *next;
-
}; // end ListNode
-
-
int size;
-
ListNode *head;
-
ListNode *find(int index) const;
-
void copyListNodes(List<T> origList);
-
-
protected:
-
-
void setSize(int newSize);
-
ListNode *getHead() const;
-
void setHead(ListNode *ptr) const;
-
T getNodeItem(ListNode *ptr) const;
-
ListNode *getNextNode(ListNode *ptr) const;
-
-
} ;// end List
-
-
template <typename T>
-
typename List<T>::ListNode *List<T>::find(int index) const
-
{
-
if ( (index < 1) || (index > getLength()) )
-
{
-
return 0;
-
}
-
-
else
-
{
-
ListNode *cur = head;
-
-
for (int skip = 1; skip < index; ++skip)
-
{
-
cur = cur->next;
-
} // end for
-
-
return cur;
-
} // end if
-
} // end find
-
-
template <typename T>
-
typename List<T>::ListNode *List<T>::getHead() const
-
{
-
return head;
-
-
} // end getHead
-
-
template <typename T>
-
typename List<T>::ListNode *List<T>::getNextNode(ListNode *ptr) const
-
{
-
return ptr->next;
-
} // end getNextNode
-
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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....
|
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...
|
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 ;
|
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...
|
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...
|
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;
|
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:...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |