2016-12-28 15 views
0

で宣言されていないメンバ関数私は、基本クラスと派生クラス初期状態 .Whenを持って、私は解決策コンパイラショーのエラーC2509を構築:「setviewコマンド」:メンバ関数は、「初期状態」で宣言されていませんエラー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:

...ここ が State.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の関数の前にキーワード「仮想」を追加しようとしたが、それはまた...何も変更はありませんInitialState.cppを削除すると、コードは正常にコンパイルされます。ここで

AtmTest.cppです: 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 

#include "PaymentCard.h" 
//#include "Atm.h" 

int main() { 
    return 0; 
} 

が、それは国家と何の関係もありません... 、ここでは他のクラスであります

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にインクインします。プリコンパイルされたヘッダーをすべて消去、再構築、または無効にします。私があるため、と思われる

:InitialState.hでIはsetviewコマンドの宣言を(コメントアウトすることによってエラーを再現できる

  1. 得られたエラーメッセージが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'