views:

63

answers:

4

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;
}
+1  A: 

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.

dark_charlie
In fact, you can name an object the same as the name of its type. You just shouldn't.
James McNellis
@James McNellis: Well, right... I'll rephrase my answer a little :)
dark_charlie
I used to think your "shouldn't" part as "couldn't". Can you give an example how I "can"?
ArunSaha
A: 

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_;
};
ArunSaha
but i have a score class that is supposed to manage the score since there will be many ways to score/loose/reset points from other obj/functions
EddieV223
@EddieV223: This answer doesn't remove your score class. It just puts the instance of score inside collisions instead of putting it as a global variable.
dark_charlie
is it possible to put an instance of that same score object in other classes too?
EddieV223
Yes, it *is* possible to put variables of type `score` in as many classes you want, however, where ever you put it, you have to "maintain" it. By "maintain", I mean keep updating it.
ArunSaha
I think im having an issue with #including the Score class in the collisions.h file, it's error says undefine class "Score" in Collisions::thescore" i changed the names of my class to Score and the variable in Collisions to thescore, but there seems to be an issue that it still doesn't know what a Score is. And if i add #include "score.h" then it errors that im redefining my Score class...
EddieV223
If you put the declarations in .h file, and definitions in .cpp file you wouldn't have to worry about any redefinition. See, for example, http://stackoverflow.com/questions/3806525/missing-type-specifier-error-on-constructor-declaration
ArunSaha
the score and the collision classes are in 2 different .h and .cpp files. maybe i should put them in the same files?
EddieV223
+1  A: 

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.

Mark Ransom
A: 

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.  
Matt H