scope of template-inherited members

Discussion in 'C++' started by filip.konvicka@gmail.com, Dec 19, 2007.

  1. Guest

    Hi, I'm having problems with the following code:

    template<typename T> struct A {
    T i;
    };

    template<typename T> struct B : public A<T> {
    void access_i() { i; }
    };

    g++ 4.2.2 complains that

    'i' was not declared in this scope

    in B<T>::access_i() definition. Of course, when I use

    void access_i() { A<T>::i; }

    everything's OK, but I don't see why I need to use this. Can anyone
    please explain why this is?

    The code compiles fine with MSVC, and I'd like to keep the code
    readable...

    Thanks,
    Corn
    , Dec 19, 2007
    #1
    1. Advertising

  2. Guest

    To answer myself... this is because of C++ standard's [temp.dep]/3,
    and the following suffices:

    void access_i() { this->i; }

    Sorry for the noise.

    Corn

    napsal:
    > Hi, I'm having problems with the following code:
    >
    > template<typename T> struct A {
    > T i;
    > };
    >
    > template<typename T> struct B : public A<T> {
    > void access_i() { i; }
    > };
    >
    > g++ 4.2.2 complains that
    >
    > 'i' was not declared in this scope
    >
    > in B<T>::access_i() definition. Of course, when I use
    >
    > void access_i() { A<T>::i; }
    >
    > everything's OK, but I don't see why I need to use this. Can anyone
    > please explain why this is?
    >
    > The code compiles fine with MSVC, and I'd like to keep the code
    > readable...
    >
    > Thanks,
    > Corn
    , Dec 19, 2007
    #2
    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. Replies:
    4
    Views:
    373
    Siemel Naran
    May 20, 2004
  2. lovecreatesbeauty
    Replies:
    43
    Views:
    1,299
    Keith Thompson
    Feb 6, 2006
  3. lovecreatesbeauty
    Replies:
    2
    Views:
    578
  4. Ian Collins
    Replies:
    7
    Views:
    455
    James Kanze
    Jul 16, 2008
  5. 7stud --
    Replies:
    11
    Views:
    399
    7stud --
    Nov 9, 2007
Loading...

Share This Page