c++ - 在 C++ 中传递对象会出现 'invalid use of non-static member' 错误

标签 c++ pointers object methods scope

所以,我正在制作一个游戏,当 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/

相关文章:

c++ - 蓝牙服务问题

c++ - 在 Windows 上构建 mongocxx 驱动程序时出错

c - 如果你 malloc 一个 struct* 它会创建局部变量吗?

java - 可能返回一个字符串数组

c++ - 处理对象组的设计模式

c++ - CMake在自定义命令中编译C++文件

c++ - C++ 中的可索引跳过列表实现

c - 分配错误 : incorrect checksum for freed object

C:从单链表1中删除一个节点并将其插入到单链表2的头部

JavaScript : How to get unique Id for multiple duplicate records