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 <string>
    #include <map>

    class A {
    private:
    typedef std::map<std::string, int> maptype_t;

    static const maptype_t _mapconst;
    static maptype_t _init_mapconst();

    }

    inline A::maptype_t
    A::_init_mapconst() // line 15
    {
    maptype_t m;
    m["aaa"] = 23;
    m["acb"] = 42;
    return m;

    }

    const A::maptype_t
    A::_mapconst =
    A::_init_mapconst();

    int main()
    {

    }

    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. * :
    > 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 <string>
    > #include <map>
    >
    > class A {
    > private:
    > typedef std::map<std::string, int> maptype_t;
    >
    > static const maptype_t _mapconst;
    > static maptype_t _init_mapconst();
    >
    > }
    >
    > inline A::maptype_t
    > A::_init_mapconst() // line 15
    > {
    > maptype_t m;
    > m["aaa"] = 23;
    > m["acb"] = 42;
    > return m;
    >
    > }
    >
    > const A::maptype_t
    > A::_mapconst =
    > A::_init_mapconst();
    >
    > int main()
    > {
    >
    > }



    1. The error message refers to a line 29, you have singled out line 15.

    2. The error message refers to an identifier 'Image', there's no such
    identifier in the presented code.


    > The problem seems to be not present if I remove the template leaving
    > as a common class the Image one.


    3. There's no template definition in the presented code.


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


    4. There's no identifier 'Handler' in the presented code.


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


    5. Post the code.

    --
    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
    #2
    1. Advertising

  3. Guest

    On 17 Feb, 01:38, "Alf P. Steinbach" <> 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 <string>
    > > #include <map>

    >
    > > class A {
    > > private:
    > > typedef std::map<std::string, int> maptype_t;

    >
    > > static const maptype_t _mapconst;
    > > static maptype_t _init_mapconst();

    >
    > > }

    >
    > > inline A::maptype_t
    > > A::_init_mapconst() // line 15
    > > {
    > > maptype_t m;
    > > m["aaa"] = 23;
    > > m["acb"] = 42;
    > > return m;

    >
    > > }

    >
    > > const A::maptype_t
    > > A::_mapconst =
    > > A::_init_mapconst();

    >
    > > int main()
    > > {

    >
    > > }

    >
    > 1. The error message refers to a line 29, you have singled out line 15.
    >
    > 2. The error message refers to an identifier 'Image', there's no such
    > identifier in the presented code.
    >
    > > The problem seems to be not present if I remove the template leaving
    > > as a common class the Image one.

    >
    > 3. There's no template definition in the presented code.
    >
    > > 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 ?

    >
    > 4. There's no identifier 'Handler' in the presented code.
    >
    > > Is a solution to templatizate also the Handler class and moreover move
    > > it outside the Image class ?

    >
    > 5. Post the code.
    >
    > --
    > 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?


    Sorry I posted the wrong code ... now you can see the correct one ...

    TY
    , Feb 17, 2007
    #3
    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,280
    Dale King
    Jul 27, 2004
  2. JKop
    Replies:
    3
    Views:
    453
  3. Carlo v. Dango
    Replies:
    14
    Views:
    1,014
    Alex Martelli
    Oct 19, 2003
  4. Pyenos
    Replies:
    2
    Views:
    378
    Pyenos
    Dec 27, 2006
  5. Replies:
    6
    Views:
    365
Loading...

Share This Page