gpt4 book ai didi

c++ - 容器作为模板参数传递给类模板

转载 作者:行者123 更新时间:2023-11-27 23:26:55 29 4
gpt4 key购买 nike

我正在尝试创建一个简单的模板类,其中我创建了一个提供容器作为模板类型的模板类对象,根据我对模板的理解,这应该没问题,应该像 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com