所以,我正在制作一个游戏,当 throw 者(球)落在蹦床上时,我需要改变 throw 者的速度。
我已经在 main
中有一个名为 detect_collision
的函数,它位于文件 main.cpp
所以,在蹦床类中,我有这个功能:
蹦床.cpp:
#include "trampoline.h"
#include "main.h"
#include "thrower.h"
//other functions
void Trampoline::landed(Thrower* thrower){
if (detect_collision(this->bounding_box, thrower->bounding_box))
thrower->speed.y = -2* thrower->speed.y;
}
在 main.cpp 中:
#include "trampoline.h"
#include "main.h"
#include "thrower.h"
// other functions
bool detect_collision(bounding_box_t a, bounding_box_t b) {
return (abs(a.x - b.x) * 2 < (a.width + b.width)) &&
(abs(a.y - b.y) * 2 < (a.height + b.height));
}
编译时出现如下错误:
error: invalid use of non-static member function ‘bounding_box_t Trampoline::bounding_box()’
if (detect_collision(this->bounding_box, thrower->bounding_box))
^
其他类似的问题没有多大帮助。
最佳答案
似乎您已将 thrower->bounding_box
定义为成员 function 而不是成员 variable。所以你需要将它用作thrower->bounding_box()
所以你的代码应该是
#include "trampoline.h"
#include "main.h"
#include "thrower.h"
//other functions
void Trampoline::landed(Thrower* thrower){
if (detect_collision(this->bounding_box, thrower->bounding_box()))
thrower->speed.y = -2* thrower->speed.y;
}
关于c++ - 在 C++ 中传递对象会出现 'invalid use of non-static member' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48426032/