Marlin Hankin
Marlin Hankin

Reputation: 61

cannot convert argument 2 from 'std::string *' to 'std::string'

Not sure why this is happening, it seems like everything is lined up the way it should be unless there is something I am missing. I can't pass these variables and no matter what I try to throw at the compiler, it just gives me another error.

This is my code so far.

void parse(string name, string storage_1, string storage_2, string storage_3)
{//some code
}

//some more code

int start = 0;
int length = 0;
string param_1, param_2, param_3;



while (!infile.eof())
{
    if (!infile)
    {
        cout << "ERROR: File " << file << " could not be located!" << endl << endl;
        break;
    }

    param_1.clear();
    param_2.clear();
    param_3.clear();
    line_input.clear();

    getline(infile, line_input);

    parse(line_input, &param_1, &param_2, &param_3);

    cout << param_1 << endl << param_2 << endl << param_3 << endl << endl;
}

Upvotes: 0

Views: 2523

Answers (3)

Oguk
Oguk

Reputation: 1137

In your call to parse, you are passing &param_1, which creates a string*, instead of plain param_1, which would be the string that your function expects.

Upvotes: 0

icemoon1987
icemoon1987

Reputation: 36

In the function declaration:

void parse(string name, string storage_1, string storage_2, string storage_3)

The second, third, and forth arguments are a string passed by value.

But in the function call:

string param_1, param_2, param_3;
parse(line_input, &param_1, &param_2, &param_3);

You are passing addresses to these strings (pointers).

You may change the declaration to:

void parse(string name, string* storage_1, string* storage_2, string* storage_3)

Or change the function call to use references:

void parse(string name, string& storage_1, string& storage_2, string& storage_3)
parse(line_input, param_1, param_2, param_3);

Upvotes: 2

Remy Lebeau
Remy Lebeau

Reputation: 596236

The error is self-explanatory. You are passing pointers where pointers are not expected. Specifically, you are using the & operator to pass your parameter values to parse(), but it does not want pointers.

Since parse() is intended to output the parsed values, you need to either:

  1. change parse() to use references, and remove the & operator when calling parse():

    void parse(string name, string &storage_1, string &storage_2, string &storage_3)
    {
        //some code
        storage_1 = ...;
        storage_2 = ...;
        storage_3 = ...;
    }
    
    parse(line_input, param_1, param_2, param_3);
    
  2. change parse() to work with pointers instead, and keep the & operator:

    void parse(string name, string *storage_1, string *storage_2, string *storage_3)
    {
        //some code
        *storage_1 = ...;
        *storage_2 = ...;
        *storage_3 = ...;
    }
    
    parse(line_input, &param_1, &param_2, &param_3);
    

If all three parameters are required, use #1. If any of the parameters can be optional use #2.

Upvotes: 0

Related Questions