LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-25-2008, 10:56 AM   #1
parv
Member
 
Registered: Jul 2004
Location: USA
Distribution: Mint, Scientifc Linux, Ubuntu
Posts: 180

Rep: Reputation: 30
c++ template function, expected constructor, destructor, or type conversion


I have the following code:
Code:
#include <list>

using namespace std;

template <typename T>
list<T>::iterator doStuff( list<T>::iterator myIter  ) //--error here
{
  list<T>::iterator iter = myIter;
.
 do_some_stuff
 .
 return iter;
 }


int main() {
 ....
  doStuff(anIterator);
...
}
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,
Code:
template <typename T>
typename myClass<T>::iterator doStuff(myClass<T>::iterator myIter)
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?
Thanks!

Last edited by parv; 01-25-2008 at 11:02 AM.
 
Old 01-25-2008, 12:10 PM   #2
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
No expert but your defintion and declaration may havvr aa mismatcch
What does you actual cod say against what the header definies. The first error is there's no consistency.

Start from the beginninging.
 
Old 01-25-2008, 12:45 PM   #3
parv
Member
 
Registered: Jul 2004
Location: USA
Distribution: Mint, Scientifc Linux, Ubuntu
Posts: 180

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by Looking_Lost View Post
No expert but your defintion and declaration may havvr aa mismatcch
What does you actual cod say against what the header definies. The first error is there's no consistency.

Start from the beginninging.
I cannot understand what you were trying to say.
If you wanted to be helpful, just give me a clue.

BTW, I did see a lot of words in your post with red underlines
which justified your statement. People willing to provide insightful
information usually have more expertises such that they could explain better.

Last edited by parv; 01-25-2008 at 01:03 PM.
 
Old 01-25-2008, 01:09 PM   #4
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Apologies.

As I said no expert but geting a bit fed up with people not puttting the effort in and wanting an easy answer to everything, which doesn't apply here, you got a genuine query. Will answer to the best of my abilities when I wake up.
 
Old 01-25-2008, 01:10 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Search online or look in your favourite cpp book for "non-deducible context":

Code:
template <typename T>
T doStuff( T myIter  )
{
  T iter (myIter);
.
 do_some_stuff
 .
 return iter;
 }
 
Old 01-25-2008, 01:12 PM   #6
parv
Member
 
Registered: Jul 2004
Location: USA
Distribution: Mint, Scientifc Linux, Ubuntu
Posts: 180

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by Looking_Lost View Post
Apologies.

As I said no expert but geting a bit fed up with people not puttting the effort in and wanting an easy answer to everything, which doesn't apply here, you got a genuine query. Will answer to the best of my abilities when I wake up.
Sorry for being a little offensive.
I've managed to change the code to:
Code:
template <typename T>
typename list<T>::iterator doStuff(typename list<T>::iterator iter) {
  typename list<T>::iterator retIter = iter;
......
  return retIter;
}
iterator appears to be the problem so that "typename" is needed ahead of it.
A better explanation from someone would be more helpful.
 
Old 01-25-2008, 01:15 PM   #7
parv
Member
 
Registered: Jul 2004
Location: USA
Distribution: Mint, Scientifc Linux, Ubuntu
Posts: 180

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by dmail View Post
Search online or look in your favourite cpp book for "non-deducible context":

Code:
template <typename T>
T doStuff( T myIter  )
{
  T iter (myIter);
.
 do_some_stuff
 .
 return iter;
 }
Note that the return type of function doStuff is an iterator, not T.
 
Old 01-25-2008, 01:15 PM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
"A better explanation from someone would be more helpful."
The reason is the type is dependant on the template parameter so the typename is required or as I post completely omit the list iterator and just use T.

"Note that the return type of function doStuff is an iterator, not T."
please note that T is the iterator

Last edited by dmail; 01-25-2008 at 01:16 PM.
 
Old 01-25-2008, 01:17 PM   #9
parv
Member
 
Registered: Jul 2004
Location: USA
Distribution: Mint, Scientifc Linux, Ubuntu
Posts: 180

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by dmail View Post
"A better explanation from someone would be more helpful."
The reason is the type is dependant on the template parameter so the typename is required or as I post completely omit the list iterator and just use T.
Got you.
Thanks for everyone's reply.
 
Old 01-25-2008, 01:21 PM   #10
parv
Member
 
Registered: Jul 2004
Location: USA
Distribution: Mint, Scientifc Linux, Ubuntu
Posts: 180

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by dmail View Post
"A better explanation from someone would be more helpful."
The reason is the type is dependant on the template parameter so the typename is required or as I post completely omit the list iterator and just use T.

"Note that the return type of function doStuff is an iterator, not T."
please note that T is the iterator
Well, I originally wanted to use T as int, double, etc. doStuff() returns an iterator
pointing to a list of type T. So I guess the word "iterator" has to appear somewhere in the function
definition, right?

Last edited by parv; 01-25-2008 at 01:24 PM.
 
Old 01-25-2008, 01:26 PM   #11
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
No you are not getting what I mean, think of it like this.
T is anything for which the function is valid
std::vector<foo>::iterator or
std::list<bar>::iterator or
my_container<some_type>::iterator

If you do not use anything iterator specific in the function then these could also be valid
int* foo* etc etc
 
Old 01-25-2008, 01:36 PM   #12
parv
Member
 
Registered: Jul 2004
Location: USA
Distribution: Mint, Scientifc Linux, Ubuntu
Posts: 180

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by dmail View Post
No you are not getting what I mean, think of it like this.
T is anything for which the function is valid
std::vector<foo>::iterator or
std::list<bar>::iterator or
my_container<some_type>::iterator

If you do not use anything iterator specific in the function then these could also be valid
int* foo* etc etc
if T is an iterator, say list<int>::iterator, then if I want to
create a list of int type, then pass an iterator to function doStuff,
what shall I do to modify my current code? Can the compiler
automatically knows since the iterator is of list<int>, then the list
to be created is also of type int?

I tried to compile the code you provided, but got some problem.

undefined reference to `std::list<int, std::allocator<int> >::iterator doStuff<int>(std::list<int, std::allocator<int> >::iterator)'
 
Old 01-25-2008, 02:15 PM   #13
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
It all depends on what containers or types you want to operate on and what operations you are preforming inside the templated function. So if you just want to use lists then there in not much use going as generic as I was showing. The following will suffice.
Code:
template<typename T>
typename std::list<T>::iterator foo(typename std::list<T>::iterator i)
{
   ....
   return it;
}
 
Old 01-25-2008, 02:20 PM   #14
LinuxManMikeC
Member
 
Registered: Nov 2007
Location: Provo, Utah
Distribution: Debian and Ubuntu
Posts: 74

Rep: Reputation: 15
You were on the right track with typename, but there were 3 places you needed it. I'm not certain of all the technical details, but apparently in this situation there is no way for the compiler to know that list<T>::iterator is a data type, not a variable or function. So you use the typename keyword to let the compiler know that list<T>::iterator is in fact a type.
Code:
template <typename T>
typename list<T>::iterator doStuff( typename list<T>::iterator myIter  )
{
   typename list<T>::iterator iter = myIter;
   ...
   return iter;
}


@dmail
I think your solution might be a little too generic for parv's needs. If his function is meant to work on an iterator and return an iterator there should be enough information available to the compiler to enforce that requirement. Just declaring everything as type T will let any data types in. Your method would eliminate compiler errors for any type that implements the operators expected in the iterator. Instead of easily found compile errors one would then have to hunt logic errors at run-time. Any compile errors that do relate to the requirement of an iterator would likely be cryptic.



P.S. Wow, you danced around the solution for a while there dmail.
 
Old 01-25-2008, 02:24 PM   #15
parv
Member
 
Registered: Jul 2004
Location: USA
Distribution: Mint, Scientifc Linux, Ubuntu
Posts: 180

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by LinuxManMikeC View Post
You were on the right track with typename, but there were 3 places you needed it. I'm not certain of all the technical details, but apparently in this situation there is no way for the compiler to know that list<T>::iterator is a data type, not a variable or function. So you use the typename keyword to let the compiler know that list<T>::iterator is in fact a type.
Code:
template <typename T>
typename list<T>::iterator doStuff( typename list<T>::iterator myIter  )
{
   typename list<T>::iterator iter = myIter;
   ...
   return iter;
}
@dmail
I think your solution might be a little too generic for parv's needs. If his function is meant to work on an iterator and return an iterator there should be enough information available to the compiler to enforce that requirement. Just declaring everything as type T will let any data types in. Your method would eliminate compiler errors for any type that implements the operators expected in the iterator. Instead of easily found compile errors one would then have to hunt logic errors at run-time. Any compile errors that do relate to the requirement of an iterator would likely be cryptic.



P.S. Wow, you danced around the solution for a while there dmail.
well said and clear enough.
dmail's solution is indeed generic but I have to get used to it.
Have not not template quite often in the past, so it takes my time.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
not calling copy constructor on function return jhorvath Programming 7 09-22-2009 12:43 PM
Does derivated class inherit base class destructor (constructor)? kornerr Programming 2 08-23-2006 08:05 AM
Compilation problem overloading operator () to template class constructor. ianix Programming 1 04-19-2006 01:16 PM
Homework Help: Copy Constructor on a template class. MicahCarrick Programming 2 01-22-2006 10:43 PM
constructor return type macro-linux Programming 2 11-12-2003 01:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration