need to convert single character to string for function call

I have a function that loops through an array of strings, trying to find a match. I read that I'm supposed to divide sizeof(array) by 4 to get the actual array counter, but it's blowing up on me, so I took the divider out. Still blowing up on me.

Here is the function, and the calling code, and the error I'm getting. Any pointers would be appreciated. I've been winnowing down a bunch of errors and this is my last one.

I hope the code works as I've constructed it(and yields only the error I'm writing about). I was taking bits and pieces from within my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22


bool found_in_array(string input, string &search_array, int sizeof_ary){
	for (int i=0; i < sizeof_ary; i++){
		if (input==&search_array[i]){
			return 1;
		}
	        else {
		    return 0;
		}
	}
}


int main() {
string po_ary[3] = {"PO", "P.O.", "Post"}; 

if (found_in_array(first_word, po_ary, sizeof(po_ary)) ){
		
                	address.po_box_yn= 1;
    	}
}


The error is:

182 61 C:\Users\DSNoS\Documents\CPP Projects\Address\main.cpp [Error] invalid initialization of non-const reference of type 'std::string& {aka std::basic_string<char>&}' from an rvalue of type 'std::string* {aka std::basic_string<char>*}'
Last edited on
Your current parameter is a reference to a single string hence you cannot pass an array. The correct parameter is this:

bool found_in_array(string input, const string search_array[], int sizeof_ary){ // Note: No reference

or equivalent

bool found_in_array(string input, const string *search_array, int sizeof_ary){ // Note: No reference

Note that const isn't necessary but makes it more safe.


To calculate the size of the array do this:

sizeof(po_ary) / sizeof(po_ary[0])
thanks.
That cannot be your code. Not the one that gives your error. First thing lacking is the definition of type string.

After that, these are the messages I get:
 In function 'int main()':
19:24: error: 'first_word' was not declared in this scope
20:5: error: 'address' was not declared in this scope
 In function 'bool found_in_array(std::string, std::string&, int)':
13:1: warning: control reaches end of non-void function [-Wreturn-type]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
using std::string;

bool found_in_array( string input, string &search_array, int sizeof_ary ) {
  for (int i=0; i < sizeof_ary; i++) {
    if (input==&search_array[i]) {
      return 1;
    }
    else {
      return 0;
    }
  }
} // warning: control reaches end of non-void function [-Wreturn-type]

int main()
{
  string po_ary[3] = {"PO", "P.O.", "Post"}; 
  // error: 'first_word' was not declared in this scope
  if ( found_in_array( first_word, po_ary, sizeof(po_ary) ) ) {
    address.po_box_yn= 1; // error: 'address' was not declared in this scope
  }
}

The compiler cannot even start to complain about your other errors.

Why do you return 0 or 1, if return type is bool? C++ has true and false. No guessing why there are numbers.

You have a condition. If it is true, you return true. If it is false, you return false.
Isn't it easier to just return the value of the condition directly? No if..else required.

You have a condition inside a loop. The condition always returns.
In other words, IF the loop condition is true on the first time, then you return something and skip the rest of the loop.
If the condition is false on start, then loop is skipped, and you return nothing?

Lets fake some to get new errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
using std::string;

bool found_in_array( string input,
                     string &search_array,
                     int sizeof_ary ) // argument 2 of 'bool found_in_array(std::string, std::string&, int)'
{
  if ( 0 < sizeof_ary ) {
    return input == &search_array[0];
  }
} // warning: control reaches end of non-void function [-Wreturn-type]

int main()
{
  string po_ary[3] = {"PO", "P.O.", "Post"}; 
  string first_word;
  if ( found_in_array( first_word,
                       po_ary,
                       sizeof(po_ary) ) )
  { // invalid initialization of non-const reference of type 'std::string&' from an rvalue of type 'std::string*'
  }
}

The po_ary is an array of string.
The function call uses it as a pointer to string.
Your function requires a string (by reference).
A pointer to type T is not a type T object.

The sizeof array is in bytes. Your function expects count of elements.
An element of your array is a string. How many bytes is one string?

How many elements does your array have? You have written it in: string po_ary[3];. That is not four.

One more time:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
using std::string;

bool found_in_array( string input,
                     string* search_array,
                     int sizeof_ary )
{
  if ( 0 < sizeof_ary ) {
    return input == &search_array[0]; // error: no match for 'operator==' (operand types are 'std::string' and 'std::string*')
  }
  return false;
}

int main()
{
  string po_ary[3] = {"PO", "P.O.", "Post"}; 
  string first_word;
  if ( found_in_array( first_word, po_ary, 3 ) )
  {
  }
}

Where did that come from? Oh, we did change a reference to a pointer.

Was it right before? You essentially had:
1
2
3
4
int foo;
int bar;
int gaz=0;
if ( foo == &bar[gaz] ) {}

Does that make sense? No.
Why didn't the compiler bark at it? Operator priority.
1
2
3
&bar[gaz]
// is same as
(&bar)[gaz]

Essentially:
1
2
3
4
5
int foo;
int bar;
int gaz=0;
int* baz = &bar
if ( foo == baz[gaz] ) {}

You probably had an error that you got rid of blindly adding code without knowledge. That is unfortunate.
Thanks for all the effort you took in analyzing this. I just wanted to thank you first before I go delve into it. Yes, I did not run that code before I sent it. I was trying to take what I thought were the pertinent bits and put them together for this post (which I explained in my original post). I evidently missed the definition of "first_word". And maybe something else.

"You probably had an error that you got rid of blindly adding code without knowledge. That is unfortunate."

Yes, you are probably correct. I had a plethora of errors after I made a few additions to the code. But, this is why I posted into the Beginners section. It's also a situation where I resorted to "thrashing", trying to address all the issues, without enough knowledge under my belt.

I took one semester in C++ two years ago, and am just getting back to it. I'm afraid I"m stumbling all over myself sometimes, particularly with how to pass array components by reference, and other referencing details. And I'm sure other details too. The code now works, at least a lot better (compiles and returns results that I still need to analyze). I will go through your post and see what further understanding I can glean. Again, thank you for all the effort you put into this.

Last edited on
Topic archived. No new replies allowed.