I'm getting a C++ compiler error that I don't understand and haven't been able to find a fix or an explanation for. Here is a code snippet demonstrating the problem.
#include <iostream>
template<class T>
class A {
public:
A(int n) {data = new T[n]; }
const T &operator()(int i) const {return data[i];}
protected:
T *data;
};
template<class T>
class B : public A<T> {
public:
B(int n) : A<T>(n) {}
T &operator()(int i) {return this->data[i]; }
//const T &operator()(int i) const {return this->data[i];} // fixes problem
};
template<class T, int N>
class C : public B<T> {
public:
C() : B<T>(N) {}
private:
};
template<class T>
void doOp(const T &v) {
std::cout << v(0) << std::endl;
}
void templateTest()
{
C<double, 3> c;
c(0) = 5;
std::cout << c(0) << std::endl;
doOp(c);
}
If I un-comment the line in class B, the code compiles and executes correctly but I don't understand why defining this operator function in class B is any different from the definition in class A.
Thanks for the help.
Bill