简体   繁体   English

错误:无法转换std :: vector <std::basic_string<char> &gt;到std :: string *

[英]error: cannot convert std::vector<std::basic_string<char> > to std::string*

Being new to C++ I have tried to created a simple void function within one of my programs in order to display an array. 作为C ++的新手,我试图在我的一个程序中创建一个简单的void函数来显示一个数组。 There is however an error as seen in the title. 但是标题中有错误。 I believe it is a problem in the fact I am trying to call it with an array in a different form than the functions parameters. 我认为这是一个问题,我试图用一个不同于函数参数的数组调用它。 I am unsure how to amend that. 我不确定如何修改它。

#include <iostream>
#include <vector>

using namespace std;

void display_array(string arr[]){
    int i;
    for (i = 0; i < sizeof(arr); i++);
        cout<<arr[i];
}

int main()
{
    string current;
    std::vector<string> paths;

    cout<<"Input paths in the form 'AB'(0 to exit)";
    cin>>current;
    while (current != "0"){
        paths.push_back(current);
        cin>>current;
    }
    display_array(paths);
}

Any help is appreciated. 任何帮助表示赞赏。

The issue is that the function display_array takes a string[] as an argument, but you are passing in a std::vector<std::string> . 问题是函数display_array接受一个string[]作为参数,但是你传入一个std::vector<std::string> You can fix this by changing the display_array function to accept a const-reference to a string vector instead of an array: 您可以通过更改display_array函数来接受对字符串向量而不是数组的const引用来解决此问题:

void display_array(const std::vector<string>& arr) {
    for (auto it = arr.begin(); it != arr.end(); it++)
        cout<<*it;
}

The reason we pass in a const-reference to the vector instead of passing by value is that we are not going to alter the vector and we don't want to copy it. 我们将const-reference传递给向量而不是传递值的原因是我们不会改变向量而我们不想复制它。 It is good practice to use const whenever you can and think about the cost of copying your arguments. 尽可能使用const并考虑复制参数的成本是一种好习惯。

The notation of function display_array existed in C before C++ was around and due to the fact that C++ was made backwardly compatible to C, it compiles in C++ too. 在C ++出现之前,函数display_array的符号存在于C中,并且由于C ++与C向后兼容,因此它也在C ++中编译。

Unfortunately it is rather dangerous because intuitively, it leads to beginners making errors like yours. 不幸的是,这是相当危险的,因为直觉上,它会导致初学者像你一样犯错误。

In reality you could substitute [] fpr a pointer in the function so it takes string*. 实际上你可以用[] fpr替换函数中的指针,这样它就需要字符串*。 And the size is also the size of a pointer, not the number of elements in the array which does not get passed in. 并且大小也是指针的大小,而不是数组中未传入的元素数。

Your options are to pass in the pointer and size, or two pointers in a range where the last one is "one past the end of sequence". 你的选择是传入指针和大小,或者在最后一个是“一个超过序列结束”的范围内的两个指针。

If you are using C++03 you have to use &arr[0] to get to the first element. 如果您使用的是C ++ 03,则必须使用&arr[0]来获取第一个元素。 In C++11 you have arr.data() as a method, which is also safe to call when the vector is empty. 在C ++ 11中,你有arr.data()作为方法,当向量为空时也可以安全地调用。 (Technically &arr[0] is undefined behaviour if the vector is empty, even if you never try to dereference this pointer). (如果向量为空,技术上&arr[0]是未定义的行为,即使您从未尝试取消引用此指针)。

Thus a correction that would allow your code to work in C++03: 因此,一个允许您的代码在C ++ 03中工作的更正:

void display_array(const string *arr, size_t size )
{
    int i;
    for (i = 0; i < size; i++) // no semicolon here..
       cout<<arr[i];
}

and call it: 并称之为:

if( !paths.empty() )
      display_array( &paths[0], paths.size() );

display_array function takes an array, should take an std::vector display_array函数接受一个数组,应该带一个std :: vector

void display_array(std::vector<string> arr) {
    for (auto s : arr)
        std::cout << s;
}

You should edit your function signature to: 您应该将您的功能签名编辑为:

void display_array(vector<string> &arr)

And: 和:

for (i = 0; i < arr.size(); i++)

暂无
暂无

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

相关问题 错误:无法转换 &#39;std::basic_string<char> &#39; 到 &#39;char&#39; 赋值 - error: cannot convert 'std::basic_string<char>' to 'char' in assignment 错误:无法转换&#39;std :: basic_string <char> 从&#39;到&#39;int&#39;分配 - error: cannot convert ‘std::basic_string<char>’ to ‘int’ in assignment 错误:无法从&#39;std :: string * {aka std :: basic_string转换 <char> *}&#39;为&#39;std :: string {aka std :: basic_string <char> }&#39;| - error: could not convert from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'| 错误:无法转换&#39;std :: string {aka std :: basic_string <char> 初始化时&#39;&#39;到&#39;char *&#39; - error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char*’ in initialization 错误无法转换 &#39;std::string {aka std::basic_string<char> }&#39; 到 &#39;char&#39; 赋值 - C++ - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'char' in assignment - C++ 转换 std::basic_string<Char> 串起来 - Convert std::basic_string<Char> to string 错误无法转换 'std::string {aka std::basic_string<char> }' 到 'int' 赋值</char> - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'int' in assignment 错误无法转换 'std::string* {aka std::basic_string<char> *}' 到 'node*' 在分配</char> - Error cannot convert 'std::string* {aka std::basic_string<char>*}' to 'node*' in assignment 错误:无法从“向量”转换“标签” <std::vector<std::__cxx11::basic_string<char> &gt;&gt;' 到 ' 向量<std::__cxx11::basic_string<char> &gt;' </std::__cxx11::basic_string<char></std::vector<std::__cxx11::basic_string<char> - error: could not convert 'tab' from 'vector<std::vector<std::__cxx11::basic_string<char> >>' to 'vector<std::__cxx11::basic_string<char>>' 无法转换&#39;std :: string {aka std :: basic_string <char> }&#39;为&#39;char&#39;作为回报 - Cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char’ in return
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM