I have a function which converts the elements of a vector to a string separated by comma. I want this function to work for numeric values as int, float or double etc. so I have made the function to receive a vector of templates:
template <typename T>
std::string ConvertToString(std::vector<T> elements)
{
std::stringstream stream;
for(int i = 0; i < elements.size()-1; i++)
{
stream << elements[i];
stream << ",";
}
stream <<elements[elements.size()-1];
return stream.str();
}
Then I declare and populate a vector:
std::vector<int> values;
values.push_back(1);
values.push_back(2);
values.push_back(3);
And try to call the function:
std::string convertedString = ConvertToString(values);
Now on the call of the above function line I receive the following error :
error C2664: 'ConvertToString' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::vector<_Ty>'
I do not understand why do I get this error since the types mentioned are the same. Can somebody explain me where am I wrong and how could I make the function call work?
Thank you very much!
Edit:
I am using Visual Studio 2012.
My full code is this:
#include<string>
#include<iostream>
#include<sstream>
#include<vector>
std::string ConvertToString(std::vector<std::string> strings);
int __cdecl main(int argc, char **argv)
{
std::vector<int> values;
values.push_back(1);
values.push_back(2);
values.push_back(3);
values.push_back(4);
values.push_back(5);
std::string convertedValues = ConvertToString(values); //here the error occurs
return 0;
}
template <typename T>
std::string ConvertToString(const std::vector<T> elements)
{
std::stringstream stream;
for(int i = 0; i < elements.size()-1; i++)
{
stream << elements[i];
stream << ",";
}
stream <<elements[elements.size()-1];
return stream.str();
}
When you write this:
std::string ConvertToString(std::vector<std::string> strings);
...before main
, you are declaring a function ConvertToString
that takes a std::vector<std::string>
and return a std::string
, so when you try to call it inside main
with a std::vector<int>
it obviously does not work.
The compiler does not see the templated version of ConvertToString
in main
, so it cannot call ConvertToString(std::vector<int>)
. Change the first declaration of ConvertToString
to:
template <typename T>
std::string ConvertToString(const std::vector<T> elements);
And as already mentioned by other answers, you should pass by const reference rather than by value:
template <typename T>
std::string ConvertToString(std::vector<T> const& elements);
This is valid C++03 so is a compiler bug or, more likely (dare I way), a malformed example. (Consider building an example from int main()
and submit to the compiler vendor).
Luckily a workaround is available which will be much better anyway. Write
template <typename T> std::string ConvertToString(const std::vector<T>& elements)
ie pass the parameter by const
reference, which will also obviate a deep copy of the vector.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.