在不同的对象函数内调用对象函数

发布于 2024-09-25 00:15:57 字数 614 浏览 2 评论 0原文

由于某种原因我似乎无法做到这一点 好的,我有 2 个对象

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

邮友 2024-10-02 00:15:57

这是你的问题:

collisions collisions;
score score;

你不应该声明一个与其类型同名的变量。将类型设置为大写,一切都应该适合您。另外,不要忘记将这两个变量的定义移到它们正在使用的函数之上。

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.

何止钟意 2024-10-02 00:15:57

您已经创建了一个全局变量 score,您显然希望 collisions::lasers 更新它。这通常是一个坏主意,但我不会在这里讨论这一点。

问题是您在定义 collisions::lasers 之后声明了 score 变量,因此它无法访问该变量。重新排列代码或将 scoreextern 声明放在顶部附近。

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.

鸢与 2024-10-02 00:15:57

在我看来,你需要一个 score 成员变量,比如 score_,在 collisions 类中,这样你就可以

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 );
}

编辑 1< /强>
澄清score_

class collisions {
  private:
    score score_;
};

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_;
};
我只土不豪 2024-10-02 00:15:57

两个问题。

正如其他人指出的那样,类名与变量相同。我不太确定你能做到这一点,或者它甚至可以编译。我的编译器当然不喜欢它。

我建议你用大写开头字母来命名你的类,并且类中的每个单词都用大写字母。所有其他字母小写。例如碰撞和碰撞分数。或 CompactDisk 等。

第二个问题是碰撞不知道您全局声明的变量分数。

您需要做的是更改碰撞构造函数以采用如下所示的分数引用变量:

class collisions
{
  public:
    collisions(score &score);
    int lasers();

  protected:
    score& score_;
}

collisions(score& score)
 : score_(score) { }

现在激光器应该引用分数成员变量

  score_.scored(100);

并且您需要像这样更改全局变量:

score the_score;
collisions the_collisions(the_score);

这当然是假设您只想要一份乐谱副本。如果您想要每个碰撞类别的分数副本,那么您将没有分数全局变量,而只需删除“&”从成员变量 Score_ 中删除引用的构造函数。

顺便说一下。

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.  

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.  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文