2016-12-28 4 views
0

У меня есть базовый класс государственный и производный класс InitialState .Когда я построить решение компилятор показать ошибки C2509: «setView»: функция-член не объявлен в «InitialState» и я не знаю, почему ... Вот 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, но это ничего не меняет ... также когда я удаляю 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; 
} 

Этот код не находится вблизи конца, но он работал, пока я не сделал SetView в InitialState, так idk, что случилось.

+3

Совет: Дон 'Поместите 'using namespace' в заголовочные файлы. Плохое mojo. –

+0

Отправьте код, вызывающий функцию. –

+1

[Код, который вы показываете, компилируется отлично) (http://coliru.stacked-crooked.com/a/ca78d2b408cb7b08). Вероятно, в коде есть проблема, которую вы нам не показываете. –

ответ

0

Проблема: InitialState.h предварительно скомпилирован, и вы l чернил до предыдущей версии InitialState.h. Очистите, перестройте и/или полностью отключите предварительно скомпилированные заголовки.

Я подозреваю, что, так как:

  1. Я могу воспроизвести ошибку, закомментировав декларацию setView() в InitialState.h
  2. Полученное сообщение об ошибке относится к строке 3 и InitialState.cpp сообщение об ошибке, которое вы отправили, относится к строке 6, что указывает на то, что опубликованный исходный код не дал этого сообщения об ошибке.

Чтобы воспроизвести ошибку, необходимо закомментировать setView() decalration из класса InitialState:

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'