Reputation: 5313
For some reason i can't seem to get this right ok i have 2 objects
class score
{
public:
int scored(int amount);
private:
int currentscore;
}
int score::scored(int amount)
{
currentscore += amount;
return 0;
}
class collisions
{
public:
int lasers();
}
// ok heres my issue
int collisions::lasers()
{
// some code here for detection
// I need to somehow call score.scored(100);
score.scored(100); // not working
score::scored(100); // not working
// how do i do that?
}
collisions collisions;
score score;
int main()
{
while (true)
{
// main loop code here..
}
return 0;
}
Upvotes: 2
Views: 149
Reputation: 38947
Two problems.
As others have pointed out, the class name is the same as the variable. I'm not so sure you can do that or it will even compile. My compiler certainly doesn't like it.
I suggest you name your classes like with an uppercase starting letter and an upper case letter for every word in the class. All other letters lower case. e.g. Collisions & Score. or CompactDisk etc.
Second problem is that collisions doesn't know anything about the variable score that you've declared globally.
What you need to do is change the collisions constructor to take a score reference variable like this:
class collisions
{
public:
collisions(score &score);
int lasers();
protected:
score& score_;
}
collisions(score& score)
: score_(score) { }
Now lasers should reference the score member variable
score_.scored(100);
And you'll need to change the global variables like this:
score the_score;
collisions the_collisions(the_score);
That is of course assuming you're only wanting ONE copy of score. If you're wanting one copy of score per collisions class then you'll not have a score global variable, but simply remove the '&' from the member variable score_ and remove the constructor function that takes a reference.
And by the way.
score.scored(100); // wrong... doesn't know anything about score, not in scope yet.
score::scored(100); // wrong. scored member isn't declared as static.
Upvotes: 0
Reputation: 20383
Seems to me that you need a score
member variable, say score_
, inside collisions
class, so that you can do
int collisions::lasers()
{
// some code here for detection
// i need to somehow call score.scored(100);
// score.scored(100); // not working
// score::scored(100); // not working
// how do i do that?
score_.scored( 100 );
}
EDIT 1
Clarifying score_
class collisions {
private:
score score_;
};
Upvotes: 0
Reputation: 308402
You've created a global variable score
that you apparently want collisions::lasers
to update. That's generally a bad idea, but I won't go into that here.
The problem is that you've declared the score
variable after the definition of collisions::lasers
, so it can't access the variable. Either rearrange the code or put an extern
declaration of score
up near the top.
Upvotes: 1
Reputation: 15164
This is your problem:
collisions collisions;
score score;
You should not declare a variable with the same name as its type. Make the types uppercase and everything should work OK for you. Also do not forget to move the definition of those two variables above the functions they are being used in.
Upvotes: 1