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.