我得到一個C++編譯器錯誤,我不明白並且一直找不到 修復或解釋。這是一個演示問題的代碼片段。繼承的模板類中的C++編譯器錯誤
#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);
}
如果我取消註釋中的B類線,代碼編譯並執行正確,但我 不明白,爲什麼在定義B級這個操作符的功能是任何不同 類A的定義
感謝您的幫助。
比爾
,什麼是錯誤訊息?你不認爲這可能有關嗎? – 2013-04-28 13:12:43