错误C2509:成员函数未在派生类中声明
error C2509: member function not declared in derived class
我有基类 state 和派生的类 intirestate .。:成员函数未在"初始状态"中声明,我不知道为什么...这是 state.h:
#ifndef STATE_H
#define STATE_H
#include<iostream>
using namespace std;
class State {
public:
State() { isPrototype = true; }
virtual void execute() = 0;
virtual void setView(ostream& screen) const = 0;
virtual void onEnter() { system("CLS"); setView(cout); }
virtual void onExit() = 0;
private:
bool isPrototype;
State* nextState;
};
#endif
initialstate.h:
#ifndef INITIAL_STATE_H
#define INITIAL_STATE_H
#include"State.h"
class InitialState : public State {
public:
void execute() {}
void onExit() {}
void setView(ostream& screen) const;
};
#endif
和 prinitystate.cpp:
#include"InitialState.h"
void InitialState::setView(ostream& screen) const {
screen << "Welcome!" << endl;
screen << "Please select what you want to do: " << endl << "1.Load card" << endl << "0.Exit" << endl;
}
我试图在initialstate.h中的函数前面添加关键词"虚拟",但它不会更改任何内容...同样,当我删除initialstate.cpp时,代码编译了normaly。
这是 atmtest.cpp:
#include "PaymentCard.h"
//#include "Atm.h"
int main() {
return 0;
}
,但没有状态...这是其他类: ATM.H:
#ifndef ATM_H
#define ATM_H
#include<iostream>
using namespace std;
class Atm {
public:
static Atm* get();
static void release() { delete instance; instance = nullptr; } //Singleton
private:
int serialNumber;
string bankName;
string location;
//Singleton:
Atm();
static Atm* instance;
Atm(const Atm& m) = delete;
Atm& operator=(const Atm& m) = delete;
Atm(Atm&&) = delete;
Atm& operator=(Atm&& m) = delete;
};
#endif
atm.cpp:
#include"Atm.h"
//Singleton:
Atm* Atm::instance = nullptr;
Atm* Atm::get() {
if (instance == nullptr) {
instance = new Atm();
}
return instance;
}
paymentcard.h:
#ifndef PAYMENT_CARD_H
#define PAYMENT_CARD_H
#include<iostream>
using namespace std;
class PaymentCard {
public:
PaymentCard(string clientName);
void addMoney(unsigned int amount) { currentAmount += amount; }
void withdrawMoney(int amount);
friend ostream& operator<< (ostream&, const PaymentCard&);
private:
static int NumberGenerator;
unsigned int serialNumber;
string clientName;
int currentAmount;
};
#endif
paymentcard.cpp:
#include"PaymentCard.h"
int PaymentCard::NumberGenerator = 0;
PaymentCard::PaymentCard(string clientName) {
currentAmount = 0;
this->clientName = clientName;
serialNumber = NumberGenerator++;
}
void PaymentCard::withdrawMoney(int amount) {
if (amount > currentAmount)cout << "Ovde ide izuzetak";
else currentAmount -= amount;
}
ostream& operator<< (ostream &os, const PaymentCard& card){
os << card.serialNumber + 1 << ". Client: " << card.clientName << endl;
return os;
}
此代码不接近完成,但是直到我在初始状态进行setView之前起作用,所以idk发生了什么。
问题:初始状态。完全清洁,重建和/或禁用预先编译的标头。
我怀疑,因为:
- 我可以通过在initialstate.h 中评论setView()的声明来重现错误
- 由此产生的错误消息是指pritialstate.cpp的第3行,您发布的错误消息是指第6行,这表明已发布的源代码未产生该错误消息。
要重现错误,必须从初始状态类评论setView()脱酸:
class InitialState : public State {
public:
void execute() {}
void onExit() {}
//void setView(ostream& screen) const;
};
然后收到以下错误消息:
1>InitialState.cpp(3): error C2509: 'setView' : member function not declared in 'InitialState'
1> c:userslacidesktopsamplesstackoverflowInitialState.h(6) : see declaration of 'InitialState'
相关文章:
- 具有多个类、派生类的C++正向声明
- 是否可以使用 using 将基类中的公共成员重新声明为派生类中的私有成员?
- 基类可以声明虚拟方法但不定义它吗?仍然在派生类中定义
- 将派生类的构造函数声明为父类的友元
- 从模板基类派生是否在派生类声明的点实例化模板
- 从基类的唯一指针中声明派生类的类型
- C++ 多态性:如果派生类中的虚函数在基类中声明了常量,是否需要将其声明为常量
- 如何将超类的受保护成员访问到其派生类. 如果已在派生类中声明了具有相同名称的函数?
- 如何声明从 QApplication 派生的 Qt 类并覆盖通知函数?
- 是否可以在引用另一个派生类的派生类中声明复制构造函数?
- 冲突的声明(外部基础与派生)
- 声明派生类的对象而不指定它
- 通过派生类模板参数指定基类模板参数之一。如何使用基类的 using 声明
- 声明嵌套基模板类实例是派生类的好友
- 是否可以在C++中声明基类,以便只能通过创建函数创建从它派生的类
- 在基类主体内声明但通过派生类召集的函数
- 如何在基本模板类中声明成员,其中类型取决于派生类的类型
- 在派生的C 类中,访问基类受保护成员作为公共的访问声明
- 在基类中使用派生类,而在基类之后声明派生类
- 在派生类或基类中,我应该在哪里声明友元类