简体   繁体   中英

c++: passing vector of pointers into a function

In a program I am writing I have a vector of pointers to try and save memory usage and although this will make my program more efficient I am having trouble passing the vector of pointers into the function direct(). Any help with the correct syntax for passing this into the function is greatly appreciated

The current error being shown is : "error cannot convert 'std::vector*> ' to 'const string '... for argument '1'... The line this error is being flagged on is the line in which the function direct is called

#include <iostream>
#include <vector>

using namespace std;

// a function used to display an array used for testing purposes
void display_array(const string *arr, size_t size )
{
    int i;
    for (i = 0; i < size; i++){
       cout<<(int(arr[i][0]))-64;
       cout<<(int(arr[i][1]))-64;
       cout<<",";
    }
}
// Takes in the connections to the start and the connections to the end and returns the connection if
//there is a direct connection else returns 0
string direct(const string *destination, char *start, size_t destination_size) {
    for (int i = 0; i<destination_size;i++)
        if ((&destination[i][0] == start) or (&destination[i][1] == start))
            return destination[i];
}

int main()
{
    string current;
    std::vector<string> paths;
    std::vector<string*> start_connections;
    std::vector<string*> destination_connections;
    char start;
    char destination;

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

    cout<<"Input starting location\n";
    cin>> start;
    cout<<"Input final destination\n";
    cin>>destination;
    for(int i = 0; i < paths.size(); i++) {
        if ((paths[i][0] == destination) or (paths[i][1] == destination))  //all connections to the destination
            destination_connections.push_back(&paths[i]); // paths stored as a pointer to paths array
        if ((paths[i][0] == start) or (paths [i][1] == start)) //all connections to the start
            start_connections.push_back(&paths[i]); // paths stored as a pointer to paths array
    }
    cout<<direct(&destination_connections,&start,destination_connections.size());

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

The compiler's telling you exactly what's wrong - a vector is not a pointer.

Ideally, you shouldn't be using pointers at all - declare your vectors as

std::vector<std::string>

and pass a reference to the function using it

... direct(const std::vector<std::string> & dest, ...)

You then just pass the vector as if by value, but the reference operator tells the compiler to just pass its address instead of the whole object.

You also get the benefit of not having to pass its size separately, as the function iterating through it can access that directly (although accessing it by index isn't really the OO way).

In C++, if you're using a naked pointer, you're probably doing it wrong ;)

You are trying to pass a vector<string*>* where a string* is expected.

Change this:

direct(&destination_connections, &start, destination_connections.size());

To this:

direct(&destination_connections[0], &start, destination_connections.size());

Or this, if you are using C++11:

direct(destination_connections.data(), &start, destination_connections.size());

That being said, or is not a valid C++ keyword, you need to use || instead. And I think you are mishandling pointers inside of display() . You need to do a code review of what you are really trying to accomplish.

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