【发布时间】:2021-11-05 22:39:13
【问题描述】:
我尝试实现一个接收参数类型 int 和 string 的函数。所以我想到了模板。
头文件
template <typename T>
class SymbolTable {
public:
void run(string filename);
void insert(T value);
}
实施 (.cpp) 文件
template <typename T>
void SymbolTable::run(string filename)
{
cout << "success" << endl;
}
// Haven't implement "insert" yet!
编译器报错:
name followed by '::' must be a class or namespace name
如果我删除模板,它工作正常。有什么建议...?
【问题讨论】:
-
试试
template <typename T> void SymbolTable<T>::run(string filename)。另见Why can templates only be implemented in the header file? -
您的标题谈到在类内部使用模板。但是您的代码显示了一个类模板。是哪个?
-
"typo": ->
template <typename T> void SymbolTable<T>::run(string filename). -
只需在头文件中进行一些编辑。抱歉,这是我的第一篇文章。
-
@TanNguyen 与许多其他第一篇文章相比,你的文章相当不错。您显示了我们需要的所有代码和确切的错误。一切都是文字!我们在第一篇文章中并不经常看到这种情况:)
标签: c++ templates compiler-errors