我不明白派生文件的声明有什么问题。请帮我找一下它的问题。 这是我的头文件的内容:
/*
* STLSandbox.h
*
* Created on: Aug 4, 2012
* Author: aksinghdce
*/
#ifndef STLSANDBOX_H_
#define STLSANDBOX_H_
namespace amit {
class STLSandbox {
public:
STLSandbox();
virtual ~STLSandbox();
};
template<typename T>
class MyCotainer{
public:
virtual void inserthere(T&) = 0;
virtual void deletehere(T&) = 0;
virtual const void printhere(T&) = 0; //promise not to modify anything
};
template<typename T>
class VectorOf: public MyContainer<T>
{
public:
virtual void inserthere(T&);
virtual void deletehere(T&);
virtual const void printhere(T&);
private:
std::vector<T&> v_data; // vector of references of type T
};
template<typename T>
void VectorOf<T>::inserthere(T& item){
v_data.push_back(item);
}
template<typename T>
void VectorOf<T>::deletehere(T& item){
v_data.pop_back(item);
}
template<typename T>
const void VectorOf<T>::printhere(T& item){
std::vector<T>::iterator i = null;
for(i=v_data.begin();i<v_data.end();i++)
{
std::cout<<*i<<std::endl;
}
}
}
#endif /* STLSANDBOX_H_ */
我正在使用gcc 4.2.1
以下是我得到的错误:
[...]/STLSandbox.h:26: error: expected template-name before ‘<’ token
[...]/STLSandbox.h:26: error: expected `{' before ‘<’ token
[...]/STLSandbox.h:26: error: expected unqualified-id before ‘<’ token
[...]/STLSandbox.h:37: error: ‘template<class T> class amit::VectorOf’ used without template parameters
[...]/STLSandbox.h: In function ‘void amit::inserthere(T&)’:
[...]/STLSandbox.h:38: error: ‘v_data’ was not declared in this scope
[...]/STLSandbox.h: At global scope:
[...]/STLSandbox.h:42: error: ‘template<class T> class amit::VectorOf’ used without template parameters
答案 0 :(得分:0)
看起来它只是一个错字。您已定义了类MyCotainer
(请注意缺少的n),但稍后使用了正确的MyContainer
。