Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > problem with inner class of a class template as member function returntype

Reply
Thread Tools

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

 
 
Fei Liu
Guest
Posts: n/a
 
      01-08-2008
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();
}
~


~


~
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-08-2008
Fei Liu wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Fei Liu
Guest
Posts: n/a
 
      01-08-2008
Victor Bazarov wrote:
> Fei Liu wrote:
>> 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


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)
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-08-2008
Fei Liu wrote:
> Victor Bazarov wrote:
>> Fei Liu wrote:
>>> 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

>
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Fei Liu
Guest
Posts: n/a
 
      01-08-2008
Victor Bazarov wrote:
> Fei Liu wrote:
>> Victor Bazarov wrote:
>>> Fei Liu wrote:
>>>> 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

>> 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

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

Fei
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
specialized member function takes precedence over generic template member function bluekite2000@gmail.com C++ 1 07-20-2005 08:58 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM



Advertisments