2014-03-31 106 views
1

我現在看起來在多態性方面存在問題。我有一個名爲Asset的基類,然後是從該類繼承的3個類 - Bond,Saving和Stock。 Asset類中有一個名爲value()的函數,它不在這個類中實現,而是在每個派生類中實現。我遇到了一些錯誤,並被告知我應該使功能virtual停止編譯器告訴我,我從來沒有實現該功能。但現在我得到錯誤Warning: control reaches end of non-void function,我的代碼將無法運行。我剛剛在代碼的開頭添加了一個cout << "Testing";,但仍然沒有任何結果。這是我的資產類,然後是其中一個實施的功能。警告:控制達到非無效功能的結束 - Qt應用程序

Asset.h

#ifndef ASSET_H 
#define ASSET_H 

#include <QString> 
#include <QDate> 

class Asset 
{ 
public: 
    Asset(QString, QDate); 
    virtual double value() {}; 
    QString getDescription(); 
    QString getType(); 
protected: 
    QString type; 
    QDate date; 
private: 
    QString description; 
}; 

#endif 

Stock.cpp value function

double Stock::value() 
{ 
    return sharePrice*numShares;  
} 

我知道這是艱難的試圖拼湊出我的代碼片段。發佈所有文件似乎不太合適,有一些。但是如果您想查看,我已將整個項目鏈接到Google雲端硬盤。​​

+0

如果您沒有在基類中實現函數,則必須將其設置爲* pure * virtual。但是由於你沒有顯示所有相關的代碼,人們只能猜測你在做什麼。請提供[SSCCE](http://sscce.org) –

+0

您有'價值'功能的多重定義。似乎你的代碼的正確方法是用'double value();'替換類中的一個,除非你的意思是它被覆蓋,在這種情況下'virtual double value();'。如果這是一個基類,它應該可能有一個虛擬析構函數。 – chris

+1

1.不要使用浮動貨幣。 2.閱讀警告並重新閱讀,直到您理解它。它表示它正在達到非void函數的末尾,並且沒有返回的值。你不能把它掛起來。這正是你的Asset :: value()函數正在做的事情。 – user3427419

回答

1

在查看您發佈的代碼時,我發現Asset::value()存在問題,因爲它不會返回任何內容,並且很可能是您顯示錯誤的原因。你有兩個選項,使其純虛

virtual double value() = 0; 

或返回默認值(可能0)

virtual double value() {return 0.0;} 

如果不能解決您的問題,您應該張貼更多的代碼。

+1

實現文件中還有另一個定義。 – chris

+0

OP肯定不想在這裏聲明一個純函數! –

+1

@chris,我原先認爲,但是實現是針對'Stock'而不是'Asset'。 –

2

您的函數有一個意外的實現(定義)。在類聲明的變化

class Asset { 
    // ... 
    virtual double value() {}; 
         // ^^ <<<<<<<<<<< You don't want this 
    // ... 
}; 

class Asset { 
    // ... 
    virtual double value() = 0; 
    // ... 
}; 

在課堂Stock重寫功能

class Stock : public Asset { 
    // ... 
    virtual double value(); 
    // ... 
}; 

實施的其餘部分是好的,因爲它以前。

0

您的代碼已修復,帶有註釋。

class Currency { 
    // Never use a floating point type for currency. 
    // Wrap, for example, Intel's DFP 
    // http://www.netlib.org/misc/intel/ 
    ... 
}; 

class Asset 
{ 
    // Since you didn't implement the copy constructor, 
    // and assignment operator, you must 
    // disable the copying. Otherwise, you must implement them all, 
    // and ideally the move constructor as well. 
    Q_DISABLE_COPY(Asset) 
public: 
    // In Qt, it's idiomatic to pass the complex types via const 
    // reference, although it's not end of the world not to do so. 
    // Both are OK, since those "complex" types are cheap to copy. 
    Asset(const QString &, const QDate &); 
        /** or **/ 
    Asset(QString, QDate); 
    // A virtual destructor is necessary for a class that 
    // can be derived from. Otherwise, inevitable type slicing will 
    // lead to bugs. Remember: anyone is free to destruct the 
    // derived class through a pointer to the base class! 
    virtual ~Asset(); 
    // This is an abstract method, declare it as such. 
    // It should also be const, since it's a getter. 
    virtual Currency value() const = 0; 
    // Getters should be const. The get prefix is redundant. 
    QString description() const; 
    QString type() const; 
protected: 
    // Member data should have an agreed-upon prefix, 
    // to prevent bugs when a local variable is referenced 
    // instead of a member, and vice-versa. 
    QString m_type; 
    QDate m_date; 
private: 
    QString m_description; 
}; 

class Stock : public Asset 
{ 
    ... 
public: 
    // Q_DECL_OVERRIDE will trigger a compiler error 
    // if we're not overriding a virtual method. 
    Currency value() const Q_DECL_OVERRIDE; 
    ... 
}; 
相關問題