2011-12-17 3 views
1

네트워크 클래스가 있는데 과부하 대상이되는 두 개의 가상 함수가 필요합니다. 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) { }; 
}; 

지금, 나는 내가 그들을 과부하 할 두 번째 클래스가 있습니다. 다음은이 클래스의 헤더는 다음과 같습니다

#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 번에 두 번 선언됩니다. 정확하게 클래스 정의? 그리고 어떤 라인 49, 53, 54 라인입니까? – hvd

+0

@hvd :'class network'에서'virtual float airtime (std :: vector freq_bins)'을 정의하는 줄을 복사 할 때 게시 된 오류가 발생합니다. 나는 네가 옳다고 생각한다. –

답변

5

순수 가상 함수 선언을 시도하십시오.

virtual float airtime() = 0; 
virtual float airtime(std::vector<int> freq_bins) = 0; 

정의가 틀리면 float을 반환하도록 지정 했더라도 아무 것도 반환하지 않습니다.

팁 : 클래스의 내부 상태를 공개해서는 안됩니다. 캡슐화에 대해 봤어.

+0

나는 아마도 느리다. return 문이 없다는 것은 경고 일 뿐이다. (함수가 호출되지 않는 한 그것은 합법적이다.) 오류의 원인은 무엇인가? –

+0

아마도 컴파일러에서 뭔가해야하고 -Wall을 사용하고 있습니다. 귀하의 코드가 그렇지 않으면 컴파일 ... http://ideone.com/E9mzL – ronag

+0

나는 당신의 의견을 이해하고 있는지 잘 모르겠다. 명확히하기 위해 나는 오류를 얻지 않으며 오류의 원인을 보지 못합니다. 게시 된 함수 정의는 형식이 잘 지정되어 있으며 컴파일러에서 승인해야합니다. 경고가 적절하다하더라도 호출되는 경우 동작이 정의되지 않을 수 있기 때문입니다. –

관련 문제