Closed. This question needs debugging details。它当前不接受答案。












想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

4年前关闭。



Improve this question




我有基类 State 和派生类 InitialState 。当我构建解决方案编译器时显示错误C2509:'setView':成员函数未在'InitialState'中声明,我不知道为什么...
这是 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

InitialState.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中的函数前面添加关键字“virtual”,但它没有任何改变...同样,当我删除I​​nitialState.cpp时,代码也可以正常编译。

这是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;
}

这段代码还没完成,但直到我在InitialState中设置了SetView时,它才起作用,所以idk发生了什么。

最佳答案

问题:InitialState.h是预编译的,并且您正在链接到InitialState.h的早期版本。完全清除,重建和/或禁用预编译头。

我怀疑是因为:

  • 我可以通过注释掉InitialState.h中setView()的声明来重现该错误。
  • 结果错误消息引用InitialState.cpp的第3行,而您发布的错误消息引用第6行,这表明发布的源代码未产生该错误消息。

  • 要重现该错误,必须从InitialState类中注释掉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:\users\laci\desktop\samples\stackoverflow\InitialState.h(6) : see declaration of 'InitialState'
    

    08-06 12:33