错误C2509:成员函数未在派生类中声明

error C2509: member function not declared in derived class

本文关键字:派生 声明 C2509 成员 函数 错误      更新时间:2023-10-16

我有基类 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发生了什么。

问题:初始状态。完全清洁,重建和/或禁用预先编译的标头。

我怀疑,因为:

  1. 我可以通过在initialstate.h
  2. 中评论setView()的声明来重现错误
  3. 由此产生的错误消息是指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'