twil
twil

Reputation: 43

C++ "error: "object missing in reference to ..." depends on gcc version

I am facing a compilation error with one gcc version (4.3.2), whereas the same code will be compiled without complaints with a newer version of gcc, e.g. 4.5.2.

The following example illustrates the problem:

class Base {
  protected:
    int member;
};

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

template<typename T>
class C : public A<T> {
    C() {
        Base::member = 1;
    }
};

int main() {
}

For 4.3.2 I am getting:

test.cpp: In constructor 'C<T>::C()':
test.cpp:4: error: object missing in reference to 'Base::member'
test.cpp:14: error: from this location

For 4.5.2 it compiles just fine.

If one compiler version accepts the code it cannot be completely wrong. My guess is that it conforms to the C++ standard, but the older compiler was lacking a proper implementation of the same.

  1. What exactly is the problem?
  2. Is there a portable way of writing that kind of code such that as much compilers as possible accept it?

Upvotes: 4

Views: 2514

Answers (1)

Mark B
Mark B

Reputation: 96281

This is definitely a bug in the earlier version of g++ but the solution is simple: Just add this-> to the expression, as this->Base::member = 1; which unconfuses the compiler.

Upvotes: 2

Related Questions