gpt4 book ai didi

c++ - 错误 C2509 : member function not declared in derived class

转载 作者:行者123 更新时间:2023-12-02 11:00:52 24 4
gpt4 key购买 nike

关闭。这个问题需要debugging details .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

5年前关闭。




Improve this question




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

初始状态.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”,但它并没有改变任何东西……当我删除 InitialState.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,所以我知道发生了什么..

最佳答案

问题: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'

    关于c++ - 错误 C2509 : member function not declared in derived class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41371279/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com