我有一个简单的c ++程序,我无法编译,虽然我试图在谷歌搜索并尝试阅读有关模板,继承和矢量,但我没有任何线索,我有什么错误我在做,有谁可以帮助我! 以下是代码:
template <class T>
class Base
{
public:
int entries;
};
template <class T>
class Derive : public Base<T *>
{
public:
int j;
void pankaj(){j = entries;}
void clear();
};
template <class T> void Derive<T>::clear()
{
int i;
int j=entries;
};
int main()
{
Derive b1;
}
我收到以下错误: pankajkk&GT; g ++ sample.cpp
sample.cpp: In member function 'void Derive<T>::pankaj()':
sample.cpp:14: error: 'entries' was not declared in this scope
sample.cpp: In member function 'void Derive<T>::clear()':
sample.cpp:22: error: 'entries' was not declared in this scope
sample.cpp: In function 'int main()':
sample.cpp:26: error: missing template arguments before 'b1'
sample.cpp:26: error: expected `;' before 'b1'
谢谢!
答案 0 :(得分:2)
您必须使用this->foo
访问模板基类中的成员变量foo
。 You may ask why.
此外,正如 Old Fox 所述,您必须在声明变量T
时指定类型b1
。
template <class T>
class Base
{
public:
int entries;
};
template <class T>
class Derive : public Base<T *>
{
public:
int j;
void pankaj(){j = this->entries;}
void clear();
};
template <class T> void Derive<T>::clear()
{
int i;
int j=this->entries;
};
int main()
{
Derive<int> b1;
}
答案 1 :(得分:1)