Reputation: 166
I wrote the following code to check the elements of a vector to see is they are equal. However, it is wrong for some reason and I cannot determine why. I do se that the "control may reach end of non-void function". Which doesn't make sense because I don't see a point where that would occur.
Any help would be greatly appreciated.
Also, this is in xCode, more specifically, the program is written in C++.
bool equalElements(vector<string> fileExtensionsArray){
for (int i=0; i<fileExtensionsArray.size(); i++){
int j=i+1;
if(fileExtensionsArray.at(i) == fileExtensionsArray.at(j)){
return false;
}
else{
return true;
}
}
}
Upvotes: 2
Views: 176
Reputation: 10405
fileExtensionsArray
is empty.If your fileExtensionsArray
is an empty std::vector
,
fileExtensionsArray.size()
will be equal to 0
.
Hence the for
loop will never be executed, hence there would be no return
statements to be executed.
To fix this, you could simply add a return false
below the end of the function definition.
bool equalElements(vector<string> fileExtensionsArray){
for (int i=0; i<fileExtensionsArray.size(); i++){
int j=i+1;
if(fileExtensionsArray.at(i) == fileExtensionsArray.at(j)){
return false;
}
else{
return true;
}
}
return false; //Empty vectors are equal? Maybe not.
}
Upvotes: 3
Reputation: 15844
Your function definition guaranteed a return value. If fileExtensionsArray
is empty, it is missing a return value in your function definition.
Upvotes: 2
Reputation: 9602
What would happen if fileExtensionsArray
is empty? The for-loop
wouldn't execute and none of your return
statements would be executed.
Upvotes: 2