gcc 4 can not resolve member var derived from base class in atemplate class

Discussion in 'C++' started by steve yee, Jul 1, 2008.

  1. steve yee

    steve yee Guest

    #include <iostream>
    #include <list>

    template<class mt_policy>
    class Base
    {
    public:
    int a;
    };

    template<class mt_policy = int>
    class signal0 : public Base<mt_policy>
    {
    public:
    void aaa()
    {
    std::cout<<"a = "<<a<<std::endl;
    }
    };

    int main()
    {
    signal0<int> p;
    p.aaa();
    }
    ~

    test.cpp: In member function ‘void signal0<mt_policy>::aaa()’:
    test.cpp:17: error: ‘a’ was not declared in this scope
     
    steve yee, Jul 1, 2008
    #1
    1. Advertising

  2. steve yee

    Guest

    On Jul 1, 9:05 am, steve yee <> wrote:
    > #include <iostream>
    > #include <list>
    >
    > template<class mt_policy>
    > class Base
    > {
    > public:
    > int a;
    >
    > };
    >
    > template<class mt_policy = int>
    > class signal0 : public Base<mt_policy>
    > {
    > public:
    > void aaa()
    > {
    > std::cout<<"a = "<<a<<std::endl;


    This is the definition of class template signal0, not an instantiation
    of it. So the compiler cannot know whether any Base<mt_policy> will
    provide an 'a'. For example, some specialization may not have an a.

    The compiler cannot assume that a missing name will be available
    through a templated base. What if there were two bases?

    The solution is to fully qualify 'a': Base<mt_policy>::a

    > }
    >
    > };
    >
    > int main()
    > {
    > signal0<int> p;
    > p.aaa();}
    >
    > ~
    >
    > test.cpp: In member function ‘void signal0<mt_policy>::aaa()’:
    > test.cpp:17: error: ‘a’ was not declared in this scope


    Ali
     
    , Jul 1, 2008
    #2
    1. Advertising

  3. steve yee wrote:
    > test.cpp: In member function ‘void signal0<mt_policy>::aaa()’:
    > test.cpp:17: error: ‘a’ was not declared in this scope


    You have to add this to your signal0 class:

    using Base<mt_policy>::a;

    I don't remember now why this was standardized like that.
     
    Juha Nieminen, Jul 1, 2008
    #3
  4. steve yee

    steve yee Guest

    so this is a standard, not a bug of gcc? but vc9 can compile it.


    On Jul 2, 2:00 am, Juha Nieminen <> wrote:
    > steve yee wrote:
    > > test.cpp: In member function ‘void signal0<mt_policy>::aaa()’:
    > > test.cpp:17: error: ‘a’ was not declared in this scope

    >
    >   You have to add this to your signal0 class:
    >
    > using Base<mt_policy>::a;
    >
    >   I don't remember now why this was standardized like that.
     
    steve yee, Jul 1, 2008
    #4
  5. steve yee wrote:
    > so this is a standard, not a bug of gcc? but vc9 can compile it.


    VC9 doesn't obey the standard in all respects.
     
    Juha Nieminen, Jul 1, 2008
    #5
  6. steve yee

    Bo Persson Guest

    Re: gcc 4 can not resolve member var derived from base class in a template class

    Juha Nieminen wrote:
    > steve yee wrote:
    >> so this is a standard, not a bug of gcc? but vc9 can compile it.

    >
    > VC9 doesn't obey the standard in all respects.


    It does respect this, if you ask it too (option /Za). Default is to
    compile old, pre-standard, Windows code.


    Bo Persson
     
    Bo Persson, Jul 1, 2008
    #6
    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. Siemel Naran
    Replies:
    4
    Views:
    824
    Micah Cowan
    Jan 12, 2005
  2. Andy Lomax
    Replies:
    5
    Views:
    489
    John Carson
    Jun 30, 2005
  3. Replies:
    1
    Views:
    411
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    399
    Victor Bazarov
    May 23, 2007
  5. xmllmx
    Replies:
    6
    Views:
    460
    James Kanze
    Feb 7, 2010
Loading...

Share This Page