2016-12-28 153 views
0

聲明我有基類國家和派生類初始化狀態。當我生成解決方案編譯器顯示錯誤C2509:「的setView」:在「初始化狀態」未聲明的成員函數我不知道爲什麼... 這裏是State.h:錯誤C2509:成員函數在派生類

#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功能前添加關鍵字「虛擬」的,但它不會改變任何東西...也當我刪除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; 
} 

此代碼是不是附近整理,但它的工作,直到我已經在初始化狀態做出的setView,所以idk發生了什麼事。

+3

提示:唐'放置'在頭文件中使用namespace'語句。壞的魔咒。 –

+0

發佈調用函數的代碼。 –

+1

[你顯示的代碼編譯正常](http://coliru.stacked-crooked.com/a/ca78d2b408cb7b08)。代碼中可能存在一個問題,您沒有向我們展示。 –

回答

0

問題:InitialState.h是預編譯的,而你是l輸入到InitialState.h的以前版本。清理,重建和/或禁用預編譯頭文件。

我懷疑,這是因爲:

  1. 我可以通過在InitialState.h註釋出的setView()的聲明
  2. 所得錯誤消息重現錯誤指線路InitialState.cpp的3和您發佈的錯誤消息引用了第6行,這表明發佈的源代碼沒有產生該錯誤消息。

要重現該錯誤,一個具有從所述初始化狀態類註釋掉的setView()decalration:

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' 
相關問題