Reputation: 94
struct prediction
{
int p1;
int p2;
};
struct player
{
vector <prediction> p;
int score=0;
string name;
};
int winner (player pla, prediction pred, int i)
{
if (pla.p[i].p1 >= p.p1)
{
pla.p[i].score +=10;
return 1;
}
return 0;
}
The error is in pla.p[i].p1
winner function the error is p was not declared in this scope . I think
the problem is in the relation between struct player
and predection
.
Can anyone find the problem ?
Upvotes: 0
Views: 1374
Reputation: 319
There's a mistake in your code.
Structure is like a blueprint of some object. We may say how that object will look like. For example we have a map of a building, that's the way building is build. Can anyone live in the map of building? No, because it is only map. Structure has same behavior. It is like a map for an object. How that object will look like?, which variables will it hold?, how much memory will it's object need. A structure itself has no memory allocated but the Object of structure holds memory area and can store things.
In your code, prediction and player has no memory but their objects i.e. pla, pred. So do not initialize your score variable in structure declaration. You may make some constructor for it. code will look like this.
struct player
{
vector <prediction> p ;
int score ;
string name ;
player(){
score = 0;
}
};
The other thing, You didn't quote where does p refer? This line makes no sense to me.
pla.p[i].score += 10;
I have structured a code for you but still have very poor idea about your logic.
struct prediction
{
int p1;
int p2;
};
struct player
{
vector <prediction> p;
int score;
string name;
player() {
score = 0;
}
};
int winner(player pla, prediction pred, int i)
{
if (pla.p[i].p1 >= pred.p1)
{
pla.score += 10;
return 1;
}
return 0;
}
Upvotes: 1
Reputation: 2520
I think the problem is p is meant to be pred as p is not declared in the function.
int winner (player pla, prediction pred, int i)
{
if (pla.p[i].p1 >= pred.p1) //changed p to pred
Upvotes: 2