简体   繁体   中英

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. 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> . You can fix this by changing the display_array function to accept a const-reference to a string vector instead of an array:

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. It is good practice to use const whenever you can and think about the cost of copying your arguments.

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.

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*. 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. In C++11 you have arr.data() as a method, which is also safe to call when the vector is empty. (Technically &arr[0] is undefined behaviour if the vector is empty, even if you never try to dereference this pointer).

Thus a correction that would allow your code to work in 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

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++)

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.

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