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

Discussion in 'C++' started by Fei Liu, Jan 8, 2008.

  1. Fei Liu

    Fei Liu Guest

    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();
    }
    ~


    ~


    ~
    Fei Liu, Jan 8, 2008
    #1
    1. Advertising

  2. Re: problem with inner class of a class template as member function return type

    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
    Victor Bazarov, Jan 8, 2008
    #2
    1. Advertising

  3. Fei Liu

    Fei Liu Guest

    Re: problem with inner class of a class template as member functionreturn type

    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)
    Fei Liu, Jan 8, 2008
    #3
  4. Re: problem with inner class of a class template as member function return type

    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
    Victor Bazarov, Jan 8, 2008
    #4
  5. Fei Liu

    Fei Liu Guest

    Re: problem with inner class of a class template as member functionreturn type

    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
    Fei Liu, Jan 8, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,684
    Thomas Weidenfeller
    Oct 12, 2005
  2. Pyenos
    Replies:
    2
    Views:
    371
    Pyenos
    Dec 27, 2006
  3. Replies:
    1
    Views:
    463
    Victor Bazarov
    Jul 20, 2005
  4. Peng Yu
    Replies:
    3
    Views:
    751
    Thomas J. Gritzan
    Oct 26, 2008
  5. Hicham Mouline
    Replies:
    0
    Views:
    414
    Hicham Mouline
    Apr 23, 2009
Loading...

Share This Page