Any way to move this function definition out of the struct body?
Apr 8, 2012 at 2:03pm UTC
I got a couple of funny errors that I wasn't sure how to deal with in the following situation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
template <int S> struct Mytempstruct
{
struct Iterator;
int x;
};
template <int S> struct Mytempstruct<S>::Iterator
{
int p;
int foo();
Iterator& operator ++();
};
template <int S> int Mytempstruct<S>::Iterator::foo() { return 0; }
template <int S> Iterator& Mytempstruct<S>::Iterator::operator ++()
{
++p;
return *this ;
}
The definition of the foo() function is fine.
The definition of operator ++, however generates an error that the return type Iterator& is not defined.
If it is instead written as:
1 2 3 4 5
template <int S> Mytempstruct<S>::Iterator& Mytempstruct<S>::Iterator::operator ++()
{
++p;
return *this ;
}
The error then becomes that the compiler expected the keyword 'typename' somewhere in the head of the definition...I tried the following
template <int S> typename Mytempstruct<S>::Iterator& Mytempstruct<S>::Iterator::operator ++()
but that seemed to cause more confusion from the compiler.
When I move the function body into the struct definition, however, it works fine. Not the end of the world, especially since in the real code the function is only a few lines, but it clutters up the interface.
Thanks for looking
Last edited on Apr 8, 2012 at 2:04pm UTC
Apr 8, 2012 at 2:45pm UTC
What compiler do you use?
This definition
1 2 3 4 5 6
template <int S>
typename Mytempstruct<S>::Iterator & Mytempstruct<S>::Iterator::operator ++()
{
++p;
return *this ;
}
is compiled without errors by MS VC++ 2010.
Apr 8, 2012 at 2:54pm UTC
You're right it does compile...I must have either made a mistake or not written to file before re-building. :)
Topic archived. No new replies allowed.