Error: Conversion from scalar to nonscalar

In summary, the conversation is about a person seeking help with a code that is giving them a compiler error. They have limited programming knowledge and include the relevant section of code. The error occurs on lines where they use the find_player function, and they eventually find out that the prototype and declaration for the function do not match.
  • #1
VinceStolen
9
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
  • #2
On what line does the error occur?
 
  • #3
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++
 
  • #4
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.
 
  • #5
I get the error "Conversion from Player* to non-scalar type Player requested
 
  • #6
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.
 
  • #7
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.
 
  • #8
Thank you that was the problem. That helped me out alot.
 

What does "scalar" and "nonscalar" mean in the context of this error?

"Scalar" refers to a single value or variable, while "nonscalar" refers to a collection or array of values or variables.

Why am I getting this error when trying to convert from a scalar to a nonscalar?

This error occurs when you are attempting to convert a single value or variable into a collection or array of values or variables. This is not possible and therefore results in the error.

How can I fix this error?

To fix this error, you will need to ensure that you are converting from a collection or array of values or variables to a single value or variable. This may involve using functions such as sum() or mean() to perform calculations on the collection and return a single value.

Can this error also occur when converting from a nonscalar to a scalar?

Yes, this error can also occur when converting from a collection or array of values or variables to a single value or variable. The same logic applies - you cannot convert multiple values into a single value.

Is there a way to prevent this error from occurring?

Yes, one way to prevent this error is to check the type of data you are attempting to convert before performing the conversion. You can use conditional statements to ensure that you are only converting from a scalar to a nonscalar or vice versa.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
3
Views
729
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
5
Views
884
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top