无法"this"指针传递到另一个类并在 CPP 中调用该类的任何方法
Unable to pass "this" pointer to another class and calling any methods of that class in CPP
在这里,我尝试使用"this"关键字将ATMMachine的实例传递给HasCard类,并尝试使用该实例从HasClass调用ATMMachine的任何方法。但是我不能调用ATMMachine的任何方法。 例如无法呼叫machine->insertCard();
有人可以帮我找出问题所在吗? CPP中是否有更好的方法可以在班级之间进行交流?
class ATMState{
virtual void insertCard() = 0;
virtual void ejectCard() = 0;
virtual void insertPin(int pinNumber) = 0;
virtual void withdrawCash(int amount) = 0;
};
class ATMMachine;
class HasCard: public ATMState {
private:
ATMMachine* machine;
public:
HasCard(ATMMachine* _machine) {
machine = _machine;
machine->insertCard();
}
void insertCard() {
}
void ejectCard() {
}
void insertPin(int pinNumber) {
}
void withdrawCash(int amount) {
}
};
class ATMMachine{
public:
int balance;
ATMState* currentState;
ATMState* hasCard;
ATMState* noCard;
ATMState* hasPin;
ATMState* noCash;
ATMMachine() {
hasCard = new HasCard(this);
// noCard = new NoCard();
// noCash = new NoCash();
// hasPin = new HasPin();
currentState = hasCard;
}
void insertCard() {
cout<<"Card has been inserted" <<endl;
}
void ejectCard() {
}
void insertPin(int pinNumber) {
}
void withdrawCash(int amount) {
}
};
But I am not able to call any of the methods of ATMMachine.
带有前向声明class ATMMachine;
你只知道这个类存在,但编译器在达到完整的类定义之前不知道它的成员函数的任何内容。
这就是您收到如下错误的原因:
invalid use of incomplete type 'class ATMMachine' machine->insertCard(); note: forward declaration of 'class ATMMachine' class ATMMachine;
如果您有这种交叉依赖项,则需要拆分成员函数、构造函数或析构函数的声明及其定义。
class ATMState {
virtual void insertCard() = 0;
virtual void ejectCard() = 0;
virtual void insertPin(int pinNumber) = 0;
virtual void withdrawCash(int amount) = 0;
};
class ATMMachine;
class HasCard : public ATMState {
private:
ATMMachine *machine;
public:
// only declare the constructor here
HasCard(ATMMachine *_machine);
void insertCard() {}
void ejectCard() {}
void insertPin(int pinNumber) {}
void withdrawCash(int amount) {}
};
class ATMMachine {
public:
int balance;
ATMState *currentState;
ATMState *hasCard;
ATMState *noCard;
ATMState *hasPin;
ATMState *noCash;
ATMMachine() {
hasCard = new HasCard(this);
// noCard = new NoCard();
// noCash = new NoCash();
// hasPin = new HasPin();
currentState = hasCard;
}
void insertCard() { cout << "Card has been inserted" << endl; }
void ejectCard() {}
void insertPin(int pinNumber) {}
void withdrawCash(int amount) {}
};
// move the definition of the HasCard constructor after the declaration of ATMMachine
HasCard::HasCard(ATMMachine *_machine){
machine = _machine;
machine->insertCard();
}
Is there any better approach in CPP to communicate between classes?
需要做这样的事情通常表明你应该重组你的代码。有多种方法可以解决这些问题,每种方法都有其优点和缺点。但这是在代码审查中要问的问题。
相关文章:
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 从不同的 cpp 调用回调函数会导致bad_function_call
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 无法"this"指针传递到另一个类并在 CPP 中调用该类的任何方法
- 在模板化的 Rcpp 函数中调用另一个 cpp 函数
- LNK2019 在文件中调用静态成员的方法时出错.cpp
- test1.cpp:9:77:错误:对"(const std::normal_distribution) <double>(std::mt19937&)"的调用不匹配
- 当指针在 cpp 17 中失去引用时,是否会调用非默认析构函数?
- C++,是否有可能/如何定义在.h和.cpp源文件中调用函数的类构造函数
- 如何在 cpp 的 main() 中调用此函数
- cpp 检查抱怨危险使用 c_str(). c_str() 返回的值在本次调用后无效,如何解决?
- 从 cpp lib 调用函数时的数据类型转换
- 如何在另一个.cpp文件中调用一个.cpp文件中的函数
- 从 python ctypes 调用 CPP 函数
- 如何从C文件中调用Cpp函数
- 从 Python 调用 cpp 函数时出现分段错误
- 使用重载>>运算符调用 CPP 构造函数
- R调用Cpp代码:全局变量未重新初始化
- 从Objective-C类.m调用.cpp文件
- 在.h中调用.cpp方法