0

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; } 
5
  • 1
    Any particular reason you're doing &param_x instead of simply param_x? Commented Oct 17, 2014 at 1:53
  • 1
    The compiler error says it: You are passing pointers to your parse function, but it expects strings. Commented Oct 17, 2014 at 1:53
  • I want to modify the param_x variables themselves Commented Oct 17, 2014 at 1:55
  • 1
    Then make the parameters of type std::string& and do not use an & when calling. In other words, call as follows: "parse(line_input, param_1, param_2, param_3)." Commented Oct 17, 2014 at 1:56
  • You should be checking the result of getline before using the string, not just checking for eof every so often. Commented Oct 17, 2014 at 1:59

3 Answers 3

2

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); 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.