Error: Conversion from scalar to nonscalar

  • Thread starter Thread starter VinceStolen
  • Start date Start date
  • Tags Tags
    Error Scalar
AI Thread Summary
The discussion centers around a compiler error encountered while coding in C++. The user is trying to implement a function to find a player in an array of structs but faces issues with function signatures and data types. The primary error arises from a mismatch between the function prototype and its definition; the prototype uses a single `Player` type, while the definition expects an array of `Player`. Additionally, there is confusion regarding the variable type for the `find` parameter, which should be a string but was mistakenly referenced as an int. The resolution involved correcting the function signature to ensure consistency and clarifying the data types used, which ultimately helped the user resolve the compiler error.
VinceStolen
Messages
9
Reaction score
0
Hi I am writing a code and I am getting a compiler error that I can not seem to figure out. I have limited programming knowledge and I have come here for help. This is the section of code I feel is relevant.

struct Player {
string name;
int goals;
int assists;
};

int find_player(Player , int , string);
void print_player( const Player &);
int main() {

Player p[50]
int size = 0;
string find;

cout << endl; << "Please enter players name: "
cin >> find;
if (find_player(p, size, find) == (-1)) {
cout << "No such player exists";
}
else {
print_player(p[find_player(p, size, find)]);
}

return 0;
}

void print_player(const Player &p) (//not important)

int find_player( Player p[ ], int size, string x) {
for (i=0, i < size, i++) {
if (p.name == x)
return i;
else
return (-1);
}
 
Last edited:
Technology news on Phys.org
On what line does the error occur?
 
The error occurs on the lines where i use the find_player function. I get two of them and they are both identical. Oh btw this is in g++
 
The signature of find_player is
find_player(Player , int , string)
But you said:
find_player(p, size, find)
Where find was an int.

retype "find" as a string.
 
I get the error "Conversion from Player* to non-scalar type Player requested
 
sorry that was me copying my program incorrectly.I can't seem to make emacs copy over to this text box. In my program it was actually a string sorry. I edited that so there is no more confusion.
 
Okay, well, also, your prototype and your declaration for find_player do not match. Contrast:
int find_player(Player , int , string);
With:
int find_player( Player p[ ], int size, string x) {

"Player" is a scalar, "Player[]" is a non-scalar.
 
Thank you that was the problem. That helped me out alot.
 
Back
Top