c++ template function, expected constructor, destructor, or type conversion
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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,
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!
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.
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.
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.
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:
"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
"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.
"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?
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
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)'
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.
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.
@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.
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.
@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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.