I have a templated function called load
for a class where I load a text file and then inside the load
function, I call another function that looks like this:
void add_edge (std::string origin, std::string destination, T value);
In order to split the text file by line, I am using a pre-defined function that returns a std::vector<std::string>
where each element can be accessed by doing this:
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
I want to pass the three words
elements into add_edge
like this: add_edge(words[0], words[1], words[2]);
but obviously this gives me compilation errors.
So I tried to just do a type cast of type T, like this:
T value = (T) words[2];
add_edge(words[0],words[1],value);
This still gave me compilation errors. I've been looking around online but nothing that I've seen really matches my situation. So how can I convert words[2]
into a type T and then pass that into add_edge
?
Edit: Here is the compilation error I'm getting.
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'
The solution was to convert words[2]
to an integer. Honestly I didn't realize it was that easy, I know almost nothing about templated types so I for sure thought I had to do something more tricky. Thank you for the help.
Aucun commentaire:
Enregistrer un commentaire