473,480 Members | 2,347 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

c++ template function error, expected constructor, destructor, or type conversion

5 New Member
I have the following code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. template <typename T>
  7. list<T>::iterator doStuff( list<T>::iterator myIter  ) //--error here
  8. {
  9.   list<T>::iterator iter = myIter;
  10. .
  11.  do_some_stuff
  12.  .
  13.  return iter;
  14.  }
  15.  
  16.  
  17. int main() {
  18.  ....
  19.   doStuff(anIterator);
  20. ...
  21. }
  22.  
While compiling it by gcc 4.2.2, I got this error message:
error: expected constructor, destructor, or type conversion before 'doStuff'
I searched online for some help and I added "typename" before <list T>
at the line labeled "--error here". Then error then became:
template declaration of ‘typename std::list<T, std::allocator<_CharT> >::iterator doStuff’
expected `)' before ‘myIter’

But if I don't use the STL list, i.e., use my own defined class name,
Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. typename myClass<T>::iterator doStuff(myClass<T>::iterator myIter)
  3.  
then the program can be compiled.

What should I know to define/use template function/class? It looks like the problem lies
somewhere between the standard class and my own class.
What should I do to get the original program working?
Jan 25 '08 #1
2 3516
weaknessforcats
9,208 Recognized Expert Moderator Expert
list<T> is called a dependent type. According to the C++ standard, if a dependent type is to be used as a type, then you must use the typename keyword.\

This should do it:
Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. typename list<T>::iterator doStuff( typename list<T>::iterator myIter  )
  3. {
  4.   typename list<T>::iterator iter = myIter;
  5. .
  6.  do_some_stuff
  7.  .
  8.  return iter;
  9.  }
  10.  
Jan 25 '08 #2
parvtb
5 New Member
list<T> is called a dependent type. According to the C++ standard, if a dependent type is to be used as a type, then you must use the typename keyword.\

This should do it:
Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. typename list<T>::iterator doStuff( typename list<T>::iterator myIter  )
  3. {
  4.   typename list<T>::iterator iter = myIter;
  5. .
  6.  do_some_stuff
  7.  .
  8.  return iter;
  9.  }
  10.  
Yes, it works now.
Thanks very much.
Jan 25 '08 #3

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

Similar topics

2
408
by: jeffp | last post by:
The following snipped of code won't compiler under either MSVC 7 or GCC (not sure which version). I can't think of any reason it should not work, anybody have any ideas: template <typename T>...
3
2227
by: Gerhard Pfeiffer | last post by:
Hi, I'm trying to implement a data-structure and have an iterator for it. Now I've got a problem impleneting the operator+. I tried to isolate the problem: template<int DIM, typename Tclass...
3
2014
by: mike.arsenault | last post by:
Hello I need some help from anyone that can provide it. Below is a function inside a template collection class that I'm writing. I have a TYPE * that points to an allocated memory location, and...
3
3735
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
4
3489
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
13
3384
by: edyam | last post by:
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...
4
1631
by: Fei Liu | last post by:
Hello, I am having trouble to properly construct the 'show' function definition. g++ 4.1.1 is giving me error messages but intel c++ compiler compiles it fine. Which one is correct? Fei ...
2
2293
by: =?ISO-8859-1?Q?Andr=E9_Luiz_Carvalho?= | last post by:
Hi there, I'm porting an application from Java to C++ / BREW so I can't use the stl, therefore, I have to implement the basic structures that the app use in Java. I'm implementing a Hashtable...
6
391
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
7051
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
6915
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
7097
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6750
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6993
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...
0
4493
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3003
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.