Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将向量的元素转换为类型tc++;_C++_Templates_Vector_Type Conversion - Fatal编程技术网

C++ 将向量的元素转换为类型tc++;

C++ 将向量的元素转换为类型tc++;,c++,templates,vector,type-conversion,C++,Templates,Vector,Type Conversion,我有一个名为load的模板函数,对于一个类,我加载一个文本文件,然后在load函数中,调用另一个如下所示的函数: void add_edge (std::string origin, std::string destination, T value); 为了按行分割文本文件,我使用了一个预定义函数,该函数返回一个std::vector,其中每个元素都可以通过以下操作进行访问: input: hello, world, 2 std::vector<std::string> wo

我有一个名为
load
的模板函数,对于一个类,我加载一个文本文件,然后在
load
函数中,调用另一个如下所示的函数:

void add_edge    (std::string origin, std::string destination, T value);
为了按行分割文本文件,我使用了一个预定义函数,该函数返回一个
std::vector
,其中每个元素都可以通过以下操作进行访问:

input: hello, world, 2
std::vector<std::string> words = my_func::split(line, ',');
std::cout << words[0]; // outputs hello
std::cout << words[1]; // outputs world
std::cout << words[2]; // outputs 2
这仍然给了我编译错误。我一直在网上四处寻找,但我所看到的一切都不符合我的情况。那么,我如何将
单词[2]
转换为类型T,然后将其传递到
add_edge

编辑:这是我得到的编译错误

error: invalid cast from type '__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}' to type 'int'
错误:从类型“_gnu_cxx::_alloc_traits::value_type{aka std::basic_string}”转换为类型“int”无效

解决方案是将
单词[2]
转换为整数。老实说,我没有意识到这有那么简单,我对模板类型几乎一无所知,所以我确实认为我必须做一些更棘手的事情。感谢您的帮助。

模板void add_edge(std::string origin,std::string destination,T值);你能告诉我们什么编译错误吗?确切地说,什么是
T
?请创建一个。@RonTang这是我得到的编译错误:
错误:类型“\uuu gnu\u cxx::\uuu alloc\u traits::value\u type{aka std::basic\u string}”的无效强制转换,输入int”
和它的
模板
@Alex如何将字符串存储在整数中?@Alex在具有多签名的函数时使用模板。在您的情况下,add_edge是
(string,string,int)
不要使用模板。但如果您有第二个类似这样的函数:
add\u edge(string,string,float)
,您可以考虑使用模板。只要搜索它,你就会得到很多不错的教程。
error: invalid cast from type '__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}' to type 'int'