2011-12-17 19 views
1

私はネットワーククラスを持っています。これは、私がオーバーロードしようとしている2つの仮想関数、つまりairtime()airtime(std::vector<int> freq_bins)です。仮想関数をオーバーロードできません

私はクラスを定義し、下部の機能:今

class Network 
{ 
    public: 

    // Properties of the network protocol 
    std::string _name; 
    std::vector<float> _channels; 
    float _bandwidth; 
    float _txtime; 

    // Properties of the specific network 
    int _id; 
    macs::mac_t _mac; 
    protocols::protocol_t _protocol; 
    bool _static; 
    float _desired_airtime; 
    float _act_airtime; 
    bool _active; 

    // Constructor 
    Network(); 
    virtual float airtime() { }; 
    virtual float airtime(std::vector<int> freq_bins) { }; 
}; 

、私はそれらをオーバーロードする2番目のクラスを持っています。ここでは、このクラスのヘッダーは次のとおりです。

#ifndef _CSMA_H_ 
#define _CSMA_H_ 

#include <string> 
#include <vector> 
#include <map> 
#include "MACs.h" 
#include "Protocols.h" 
#include "Network.h" 

class CSMA : public Network 
{ 
    public: 

    float center_freq; 

    CSMA(); 
    float airtime(); 
    float airtime(std::vector<int> freq_bins); 
}; 

#endif 

私はその後CSMA.cppでそれらを定義します。

#include <iostream> 
#include <string> 
#include <vector> 
#include "MACs.h" 
#include "Protocols.h" 
#include "Network.h" 
#include "Simulator.h" 
#include "CSMA.h" 

extern Simulator sim; 

CSMA::CSMA() { 
    _mac = macs::CSMA; 
} 

float CSMA::airtime() { 
    return _act_airtime; 
} 

float CSMA::airtime(std::vector<int> freq_bins) { 
    return 1; 
} 

私は値を返すに警告を取得し、それは問題ではありません。しかし、これをコンパイルしようと、私は理解していないときに私が取得エラー:

g++ -o hce_sim hce_sim.cpp Network.cpp CSMA.cpp -Wall 
In file included from hce_sim.cpp:2: 
Network.h:54: error: ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’ cannot be overloaded 
Network.h:49: error: with ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’ 
Network.h: In member function ‘virtual float Network::airtime()’: 
Network.h:53: warning: no return statement in function returning non-void 
Network.h: In member function ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’: 
Network.h:54: warning: no return statement in function returning non-void 
In file included from Network.cpp:6: 
Network.h:54: error: ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’ cannot be overloaded 
Network.h:49: error: with ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’ 
Network.h: In member function ‘virtual float Network::airtime()’: 
Network.h:53: warning: no return statement in function returning non-void 
Network.h: In member function ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’: 
Network.h:54: warning: no return statement in function returning non-void 
In file included from CSMA.cpp:6: 
Network.h:54: error: ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’ cannot be overloaded 
Network.h:49: error: with ‘virtual float Network::airtime(std::vector<int, std::allocator<int> >)’ 

は、私が試してみて、私はこのエラーを取得する理由を理解するために、より単純化されたプログラムを作成し、まだこの単純化されたプログラムが動作します:

#include <iostream> 
#include <vector> 

class Base { 

    public: 
    Base() { } 
    virtual void check() { } 
    virtual void check(bool it) { } 
}; 

class First : public Base { 
    public: 

    First() { } 
    void check() { 
     std::cout << "You are in check(void) !\n"; 
    } 

    void check(bool it) { 
     std::cout << "You are in check(bool) !\n"; 
    } 
}; 

int main() { 

    First f; 
    f.check(); 
    f.check(true); 
} 

誰にも洞察はありますか?

+0

あなたが見ているエラーはありません。どのバージョンのgccを使用していますか? –

+0

愚かな質問かもしれませんが、 'virtual float airtime(std :: vector freq_bins)'はNetwork.hの49行目と54行目に2回宣言されています。コピーしたことを再度確認できますかクラス定義は正確ですか?そしてどのラインがライン49,53,54ですか? – hvd

+0

@hvd: 'class network'に' virtual float airtime(std :: vector freq_bins) 'を定義している行を複製すると、投稿されたエラーが出ます。私はあなたが正しいと思う。 –

答えて

5

純粋な仮想関数宣言を試してください。

なぜなら、定義が間違っていて、floatを返すように指定しても何も返されないからです。

ヒント:実際には、そのようなクラスの内部状態を公開しないでください。カプセル化についてグーグルでやってください。

+0

私はおそらく遅いです、リターンステートメントの欠如は、警告です(関数が呼び出されない限り、それは合法です)、エラーの理由は何ですか? –

+0

これはおそらくあなたのコンパイラで何かをしなければならないし、あなたが-Wallを使っていることでしょう。あなたのコードはそうでなければコンパイルします... http://ideone.com/E9mzL – ronag

+0

私はあなたのコメントを理解しているか分からない。明らかにするために、私はエラーを出すことはありませんし、エラーの理由は表示されません。投稿された関数定義は整形式であり、適合したコンパイラによって受け入れられなければならないが、警告が適切であるとは言えない。 –

関連する問題