c++编译错误(试图创建向量的静态向量)
C++ Compiler Error (trying to create a static vector of vectors)
我试图创建一个静态向量向量,我发现以下代码在gcc-4.1.2下编译和运行,但在gcc-4.5.1下它无法编译消息
assign.cxx:19:48: error: no matching function for call to ‘to(boost::assign_detail::generic_list<std::basic_string<char> >&)’
谁能解释一下为什么会这样?如果你对如何做我正在尝试做的事情有任何其他建议,那么我很乐意接受:)。
#include <iostream>
#include <string>
#include <vector>
#include <boost/assign/list_of.hpp>
template <template <typename> class containerType, typename elemType>
containerType<elemType> to( boost::assign_detail::generic_list<elemType> list ) {
containerType<elemType> tempContainer = list;
return tempContainer;
}
static const std::vector<std::vector<std::string> > names = boost::assign::list_of
(to<std::vector>(boost::assign::list_of<std::string>("A")("B")("C") ))
(to<std::vector>(boost::assign::list_of<std::string>("D")("E")("F") ));
int main() {
for( std::vector< std::vector<std::string> >::const_iterator itVec = names.begin(); itVec != names.end(); ++itVec ) {
for( std::vector<std::string>::const_iterator itStr = itVec->begin(); itStr != itVec->end(); ++itStr ) {
std::cout << "Value is " << *itStr << std::endl;
}
}
return 0;
}
问题源于这样一个事实,即在to<>()
的定义中,containerType
被声明为只有一个模板参数,而实际上std::vector<>
有两个模板参数。
这个在GCC 4.1.2中编译的事实只表明GCC 4.1.2中有一个错误——代码本质上仍然是不正确的。
不幸的是,我想不出一个好的解决方法。下面的代码编译,但将您限制为只有两个模板参数的容器(其中第二个是分配器),这可能不是您最终想要的:#include <iostream>
#include <string>
#include <vector>
#include <boost/assign/list_of.hpp>
template<
template<typename, typename> class containerType,
typename allocatorType,
typename elemType
>
containerType<elemType, allocatorType >
to(boost::assign_detail::generic_list<elemType> const& list)
{
return list; // no need to explicitly construct a containerType instance
}
template<template<typename, typename> class containerType, typename elemType>
containerType<elemType, std::allocator<elemType> >
to(boost::assign_detail::generic_list<elemType> const& list)
{
return to<containerType, std::allocator<elemType> >(list);
}
static std::vector<std::vector<std::string> > names = boost::assign::list_of
(to<std::vector>(boost::assign::list_of<std::string>("A")("B")("C")))
(to<std::vector>(boost::assign::list_of<std::string>("D")("E")("F")));
int main()
{
typedef std::vector<std::vector<std::string> >::const_iterator outer_iter_t;
typedef std::vector<std::string>::const_iterator inner_iter_t;
for (outer_iter_t itVec = names.begin(); itVec != names.end(); ++itVec)
for (inner_iter_t itStr = itVec->begin(); itStr != itVec->end(); ++itStr)
std::cout << "Value is " << *itStr << 'n';
}
EDIT(响应dribeas的评论):更新,允许调用者覆盖默认的分配器
在解决了几个问题之后,我认为最简单的解决方案不是使用模板模板参数,而是使用类型模板参数:
template <typename containerType, typename elemType>
containerType to( boost::assign_detail::generic_list<elemType> list ) {
containerType tempContainer = list;
return tempContainer;
}
static const std::vector<std::vector<std::string> > names = boost::assign::list_of
(to<std::vector<std::string> >(boost::assign::list_of<std::string>("A")("B")("C") ))
(to<std::vector<std::string> >(boost::assign::list_of<std::string>("D")("E")("F") ));
它可能需要在调用端输入更多的内容,但它会处理您没有在模板模板参数中声明的额外模板参数。
对函数to<std::vector>( ... )
的调用只包含一个模板参数,而函数声明包含两个模板参数。我会尝试:to<std::vector, std::string>( ... )
相关文章:
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何从静态向量访问类元素?
- 如何将对象添加到类中的静态向量
- 使用 C++98 标准填充二维静态向量
- C++ 入门 12.1:我可以在类中使用静态向量成员实现 StrBlob<string> 吗?
- DLL – 在 DLL 初始化时填充的静态向量,向客户端程序返回零大小
- 如何通过参考传递向量的静态向量
- 无法在 c++ 中初始化静态向量
- C++类中的静态向量
- 静态常量 std::<char>没有堆的向量初始化?
- 静态"global"向量似乎未填充
- C++将局部变量添加到静态向量而不复制
- 当push_back到向量中时,我是否需要复制构造函数来修改静态成员
- 包含多态对象的向量:静态断言误差
- 必须调用对非静态成员函数的引用(向量)
- 如何在C 中创建指向向量的静态指针
- 动态增长的向量,在 cpp 中声明为静态
- 大小在运行时确定的静态数组的向量
- 在迭代向量的 for 循环中无效使用非静态数据成员
- C :如何在类内声明一个空的私人静态向量