【发布时间】:2015-04-26 10:30:32
【问题描述】:
我有一个简单的 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> 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'
谢谢!!
【问题讨论】:
-
这些空白是怎么回事?
-
删除了多余的空格。
-
@KarolyHorvath 确实有效,但为什么没有
this->就不能工作? FWIWBase<T*>::entries也可以。 -
@KarolyHorvath 在将条目更改为 this-> 条目后它起作用了。非常感谢:)
标签: c++ templates c++11 inheritance g++