Problem with inner classes and Templates

Discussion in 'C++' started by alexroat@gmail.com, Feb 17, 2007.

  1. Guest

    Hi, when I'm trying to compile this code with gcc 4 I get this strange
    error :

    alex@deimos:~/Desktop/Image$ make -k Image
    g++ Image.cpp -o Image
    Image.cpp:29: error: expected initializer before 'Image'
    make: *** [Image] Error 1


    The code of the source of Image.cpp is the following:

    #include <iostream>

    template <class T>
    class Image
    {
    public:

    class Handler
    {
    public:
    private:
    Handler(class Image &i);
    friend class Image;
    };


    const Handler operator()();

    private:


    };





    template <class T>
    const Image<T>::Handler Image<T>::eek:perator()()
    {
    return Handler(*this);
    }


    template <class T>
    Image<T>::Handler::Handler(class Image<T> &i)
    {

    }



    typedef unsigned char byte;

    int main()
    {
    Image <byte> i1;


    }



    The problem seems to be not present if I remove the template leaving
    as a common class the Image one. I think it's due by the fact that
    compiler cannot predict the form of Handler before it has not been
    istantiated ... but I'm not sure ... could you help me ?

    Is a solution to templatizate also the Handler class and moreover move
    it outside the Image class ?

    Thank you in advance.

    Alessandro
     
    , Feb 17, 2007
    #1
    1. Advertising

  2. Guest

    On 17 Feb, 01:33, wrote:
    > Hi, when I'm trying to compile this code with gcc 4 I get this strange
    > error :
    >
    > alex@deimos:~/Desktop/Image$ make -k Image
    > g++ Image.cpp -o Image
    > Image.cpp:29: error: expected initializer before 'Image'
    > make: *** [Image] Error 1
    >
    > The code of the source of Image.cpp is the following:
    >
    > #include <iostream>
    >
    > template <class T>
    > class Image
    > {
    > public:
    >
    > class Handler
    > {
    > public:
    > private:
    > Handler(class Image &i);
    > friend class Image;
    > };
    >
    > const Handler operator()();
    >
    > private:
    >
    > };
    >
    > template <class T>
    > const Image<T>::Handler Image<T>::eek:perator()()
    > {
    > return Handler(*this);
    >
    > }
    >
    > template <class T>
    > Image<T>::Handler::Handler(class Image<T> &i)
    > {
    >
    > }
    >
    > typedef unsigned char byte;
    >
    > int main()
    > {
    > Image <byte> i1;
    >
    > }
    >
    > The problem seems to be not present if I remove the template leaving
    > as a common class the Image one. I think it's due by the fact that
    > compiler cannot predict the form of Handler before it has not been
    > istantiated ... but I'm not sure ... could you help me ?
    >
    > Is a solution to templatizate also the Handler class and moreover move
    > it outside the Image class ?
    >
    > Thank you in advance.
    >
    > Alessandro


    In declaration is missing <T> after Image, but I get the same error...
    could you help me ?
     
    , Feb 17, 2007
    #2
    1. Advertising

  3. * :
    >
    > template <class T>
    > const Image<T>::Handler Image<T>::eek:perator()()


    Add 'typename'.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Feb 17, 2007
    #3
  4. Guest

    On 17 Feb, 01:43, "Alf P. Steinbach" <> wrote:
    > * :
    >
    >
    >
    > > template <class T>
    > > const Image<T>::Handler Image<T>::eek:perator()()

    >
    > Add 'typename'.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?


    Where I have to put it ? Is it not the same as "class" keyword ?
    Could you write me the correct code ?

    Thank you
     
    , Feb 17, 2007
    #4
  5. Michael Guest

    On Feb 16, 4:33 pm, wrote:
    > Hi, when I'm trying to compile this code with gcc 4 I get this strange
    > error :
    >
    > alex@deimos:~/Desktop/Image$ make -k Image
    > g++ Image.cpp -o Image
    > Image.cpp:29: error: expected initializer before 'Image'
    > make: *** [Image] Error 1


    <snip>

    > template <class T>
    > const Image<T>::Handler Image<T>::eek:perator()()
    > {
    > return Handler(*this);
    >
    > }
    >


    Here's VS output. It seems more comprehensible to me:
    1>image.cpp(30) : warning C4346: 'Image<T>::Handler' : dependent name
    is not a type
    1> prefix with 'typename' to indicate a type
    1>image.cpp(30) : error C2143: syntax error : missing ';' before
    'Image<T>::()'
    1>image.cpp(30) : error C4430: missing type specifier - int assumed.
    Note: C++ does not support default-int

    So basically,
    const Image<T>::Handler Image<T>::eek:perator()()
    should be
    const typename Image<T>::Handler Image<T>::eek:perator()()

    It then compiles for me with VS & g++.

    Michael
     
    Michael, Feb 17, 2007
    #5
  6. Piyo Guest

    Here's your solution. Enjoy!
    (Missing keyword typename)

    template <class T>
    const typename Image<T>::Handler Image<T>::eek:perator()()
    {
    return Handler(*this);

    }

    wrote:
    > On 17 Feb, 01:33, wrote:
    >> Hi, when I'm trying to compile this code with gcc 4 I get this strange
    >> error :
    >>
    >> alex@deimos:~/Desktop/Image$ make -k Image
    >> g++ Image.cpp -o Image
    >> Image.cpp:29: error: expected initializer before 'Image'
    >> make: *** [Image] Error 1
    >>
    >> The code of the source of Image.cpp is the following:
    >>
    >> #include <iostream>
    >>
    >> template <class T>
    >> class Image
    >> {
    >> public:
    >>
    >> class Handler
    >> {
    >> public:
    >> private:
    >> Handler(class Image &i);
    >> friend class Image;
    >> };
    >>
    >> const Handler operator()();
    >>
    >> private:
    >>
    >> };
    >>
    >> template <class T>
    >> const Image<T>::Handler Image<T>::eek:perator()()
    >> {
    >> return Handler(*this);
    >>
    >> }
    >>
    >> template <class T>
    >> Image<T>::Handler::Handler(class Image<T> &i)
    >> {
    >>
    >> }
    >>
    >> typedef unsigned char byte;
    >>
    >> int main()
    >> {
    >> Image <byte> i1;
    >>
    >> }
    >>
    >> The problem seems to be not present if I remove the template leaving
    >> as a common class the Image one. I think it's due by the fact that
    >> compiler cannot predict the form of Handler before it has not been
    >> istantiated ... but I'm not sure ... could you help me ?
    >>
    >> Is a solution to templatizate also the Handler class and moreover move
    >> it outside the Image class ?
    >>
    >> Thank you in advance.
    >>
    >> Alessandro

    >
    > In declaration is missing <T> after Image, but I get the same error...
    > could you help me ?
    >
     
    Piyo, Feb 17, 2007
    #6
  7. Guest

    On 17 Feb, 02:07, Piyo <> wrote:
    > Here's your solution. Enjoy!
    > (Missing keyword typename)
    >
    > template <class T>
    > const typename Image<T>::Handler Image<T>::eek:perator()()
    > {
    > return Handler(*this);
    >
    > }
    > wrote:
    > > On 17 Feb, 01:33, wrote:
    > >> Hi, when I'm trying to compile this code with gcc 4 I get this strange
    > >> error :

    >
    > >> alex@deimos:~/Desktop/Image$ make -k Image
    > >> g++ Image.cpp -o Image
    > >> Image.cpp:29: error: expected initializer before 'Image'
    > >> make: *** [Image] Error 1

    >
    > >> The code of the source of Image.cpp is the following:

    >
    > >> #include <iostream>

    >
    > >> template <class T>
    > >> class Image
    > >> {
    > >> public:

    >
    > >> class Handler
    > >> {
    > >> public:
    > >> private:
    > >> Handler(class Image &i);
    > >> friend class Image;
    > >> };

    >
    > >> const Handler operator()();

    >
    > >> private:

    >
    > >> };

    >
    > >> template <class T>
    > >> const Image<T>::Handler Image<T>::eek:perator()()
    > >> {
    > >> return Handler(*this);

    >
    > >> }

    >
    > >> template <class T>
    > >> Image<T>::Handler::Handler(class Image<T> &i)
    > >> {

    >
    > >> }

    >
    > >> typedef unsigned char byte;

    >
    > >> int main()
    > >> {
    > >> Image <byte> i1;

    >
    > >> }

    >
    > >> The problem seems to be not present if I remove the template leaving
    > >> as a common class the Image one. I think it's due by the fact that
    > >> compiler cannot predict the form of Handler before it has not been
    > >> istantiated ... but I'm not sure ... could you help me ?

    >
    > >> Is a solution to templatizate also the Handler class and moreover move
    > >> it outside the Image class ?

    >
    > >> Thank you in advance.

    >
    > >> Alessandro

    >
    > > In declaration is missing <T> after Image, but I get the same error...
    > > could you help me ?


    WOW !!!
    Fantastic !!!
    Thank you ...

    I'll offer you a beer :-D

    CIAO
     
    , Feb 17, 2007
    #7
    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. Razvan
    Replies:
    5
    Views:
    11,316
    Dale King
    Jul 27, 2004
  2. JKop
    Replies:
    3
    Views:
    484
  3. Carlo v. Dango
    Replies:
    14
    Views:
    1,046
    Alex Martelli
    Oct 19, 2003
  4. Pyenos
    Replies:
    2
    Views:
    392
    Pyenos
    Dec 27, 2006
  5. Replies:
    2
    Views:
    274
Loading...

Share This Page