我正在尝试创建一个简单的模板类,其中我创建了一个提供容器作为模板类型的模板类对象,根据我对模板的理解,这应该没问题,应该像 int 或 char 一样处理,但它总是给我一个错误说:
"template argument 1 is invalid"
这是我遇到此错误的行:
templateTest<(std::list<int>)> testingTheTemplate;
这是模板类的骨架
template <class testType> class templateTest
{
/* use some iterators and print test data here */
};
我在这里错过了什么?
最佳答案
你忘记了类定义后的分号:
template <class testType> class templateTest
{
}; // <- semicolon
此外,将您的实例化声明为:
templateTest<std::list<int> > testingTheTemplate;
// ^^^ required space (C++03)
没有括号,注意中间的空格。
在 C++11 之前,<<
和 >>
被视为运营商。在这种情况下,您必须将它们分开。
关于c++ - 容器作为模板参数传递给类模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8601060/