473,385 Members | 1,846 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,385 software developers and data experts.

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

5
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 3511
weaknessforcats
9,208 Expert Mod 8TB
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
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
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
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
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
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
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
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
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
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
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>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.