0

I've just been trying to port something on windows to linux. The following example compiles just fine in VS2010 and fails in g++ with main.cpp:17:31: error: ‘data’ was not declared in this scope (g++ (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8)). Is there some workaround for accessing the base class without a tonne of casting? Is this even supported by the standard?

#include <stdio.h>

class A {
public:
    int data; 
    A() {data = 42;}
};

template<typename T>
class B : public A {
};

template<typename T>
class C : public B<T> {
public:
    void print() {printf("%i\n", data);}
};

int main()
{
    C<char> c;
    c.print();
    return 0;
}

[EDIT] The following change compiles fine in gcc.

    void print() {printf("%i\n", this->data);}
jozxyqk
  • 16,424
  • 12
  • 91
  • 180
  • `A::data` would be fine. Not sure about standard. – keltar Oct 25 '13 at 08:09
  • Your code is not valid C++, but relies on an VS extension. Make it valid and it should compile. – Walter Oct 25 '13 at 08:16
  • @Walter by valid do you mean use `this->data` as in my edit or `A::data` or is there another more common method? – jozxyqk Oct 25 '13 at 08:34
  • @jozxyqk Either is okay. Another option is to declare `using A::data;` inside `class B`. – Walter Oct 25 '13 at 16:52
  • @Walter thanks, I had to declare `using B::data;` in `class C`, but you put me on the right track. This is quite handy to know as it fixes the problem with one line rather than having to go through pasting `this` or `A::` everywhere. – jozxyqk Oct 25 '13 at 17:09

0 Answers0