如何在类之外的成员函数中调用成员函数

How do you call a member function in a member function outside of a class?

本文关键字:成员 函数 调用      更新时间:2023-10-16

我的类定义在我的头文件中,在我的。cpp文件中我有:

bool Card::foo(const std::string &trump) const {...}
bool Card::bar(const std::string &trump) const {
     bool oof = foo(const std::string &trump); 
}

由于某些原因不能工作。XCode给出了一个错误:期望表达式。当我尝试:

时也是如此
bool oof = Card::foo(const std::string &trump);
bool oof = foo(const std::string &trump) const;

检查

bool Card::foo(const std::string &trump) const {...}
bool Card::bar(const std::string &trump) const {
     bool oof = foo(trump); 
}

下列任意表达式:

bool oof = foo(const std::string &trump);
bool oof = Card::foo(const std::string &trump);
bool oof = foo(const std::string &trump) const;

将重新定义trump,因为

bool Card::bar(const std::string &trump) const 

已经定义了

调用foo (const std::string部分)时不需要/不允许类型信息,因为语言不是这样工作的。函数声明将需要它,但在调用它时不包括类型。看一下Igor的例子。