在不同的对象函数内调用对象函数
由于某种原因我似乎无法做到这一点 好的,我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是你的问题:
你不应该声明一个与其类型同名的变量。将类型设置为大写,一切都应该适合您。另外,不要忘记将这两个变量的定义移到它们正在使用的函数之上。
This is your problem:
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.
您已经创建了一个全局变量
score
,您显然希望collisions::lasers
更新它。这通常是一个坏主意,但我不会在这里讨论这一点。问题是您在定义
collisions::lasers
之后声明了score
变量,因此它无法访问该变量。重新排列代码或将score
的extern
声明放在顶部附近。You've created a global variable
score
that you apparently wantcollisions::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 ofcollisions::lasers
, so it can't access the variable. Either rearrange the code or put anextern
declaration ofscore
up near the top.在我看来,你需要一个
score
成员变量,比如score_
,在collisions
类中,这样你就可以编辑 1< /强>
澄清
score_
Seems to me that you need a
score
member variable, sayscore_
, insidecollisions
class, so that you can doEDIT 1
Clarifying
score_
两个问题。
正如其他人指出的那样,类名与变量相同。我不太确定你能做到这一点,或者它甚至可以编译。我的编译器当然不喜欢它。
我建议你用大写开头字母来命名你的类,并且类中的每个单词都用大写字母。所有其他字母小写。例如碰撞和碰撞分数。或 CompactDisk 等。
第二个问题是碰撞不知道您全局声明的变量分数。
您需要做的是更改碰撞构造函数以采用如下所示的分数引用变量:
现在激光器应该引用分数成员变量
并且您需要像这样更改全局变量:
这当然是假设您只想要一份乐谱副本。如果您想要每个碰撞类别的分数副本,那么您将没有分数全局变量,而只需删除“&”从成员变量 Score_ 中删除引用的构造函数。
顺便说一下。
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:
Now lasers should reference the score member variable
And you'll need to change the global variables like this:
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.