简体   繁体   English

相同类型之间的转换无效

[英]Invalid conversion between the same types

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: 我希望此函数适用于int,float或double等数字值,因此我使函数可以接收模板向量:

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>' 错误C2664:'ConvertToString':无法将参数1从'std :: vector <_Ty>'转换为'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. 我正在使用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. ...在main之前,您要声明一个函数ConvertToString ,该函数采用std::vector<std::string>并返回std::string ,因此当您尝试在main内部使用std::vector<int>调用它时std::vector<int>显然不起作用。

The compiler does not see the templated version of ConvertToString in main , so it cannot call ConvertToString(std::vector<int>) . 编译器在main中看不到ConvertToString的模板版本,因此无法调用ConvertToString(std::vector<int>) Change the first declaration of ConvertToString to: 将第一个ConvertToString声明更改为:

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: 正如其他答案已经提到的那样,您应该通过const引用而不是按值传递:

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. 这是有效的C ++ 03,也是编译器错误,或更可能是(格式错误)一个错误的示例。 (Consider building an example from int main() and submit to the compiler vendor). (考虑从int main()构建示例并提交给编译器供应商)。

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. 即通过const引用传递参数,这也将消除向量的深层副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM