problem with inner class of a class template as member function returntype

F

Fei Liu

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

#include <iostream>
using namespace std;

template <typename T>
struct C{
struct S{
T x;
};

S s;
S show();
};

template <typename T>
C<T>::S C<T>::show(){
cout << s.x << '\n';
return s;
}

int main(){

C<int> c;
c.show();
}
~


~


~
 
V

Victor Bazarov

Fei said:
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

#include <iostream>
using namespace std;

template <typename T>
struct C{
struct S{
T x;
};

S s;
S show();
};

template <typename T>
C<T>::S C<T>::show(){
cout << s.x << '\n';
return s;
}

int main(){

C<int> c;
c.show();
}
~

Seems fine to me. What error do you get from gcc?

BTW, Comeau online accepts it as well.

V
 
F

Fei Liu

Victor said:
Seems fine to me. What error do you get from gcc?

BTW, Comeau online accepts it as well.

V

g++ -fstrict-aliasing -fomit-frame-pointer -Wall -pedantic -ansi -g -O0
-o templ_inner_class.o -c templ_inner_class.cpp
templ_inner_class.cpp:15: error: expected constructor, destructor, or
type conversion before ‘C’
gcc version 4.1.2 20061115 (prerelease)
 
V

Victor Bazarov

Fei said:
g++ -fstrict-aliasing -fomit-frame-pointer -Wall -pedantic -ansi -g
-O0 -o templ_inner_class.o -c templ_inner_class.cpp
templ_inner_class.cpp:15: error: expected constructor, destructor, or
type conversion before 'C'
gcc version 4.1.2 20061115 (prerelease)

Perhaps a 'typename' would silence it:

template <typename T>
typename // this here
C<T>::S C<T>::show(){ ...

V
 
F

Fei Liu

Victor said:
Perhaps a 'typename' would silence it:

template <typename T>
typename // this here
C<T>::S C<T>::show(){ ...

V
Yeap, this is the fix for g++, thanks!

Fei
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top