네트워크 클래스가 있는데 과부하 대상이되는 두 개의 가상 함수가 필요합니다. 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);
}
여기에 통찰력이있는 사람이 있습니까?
오류가 표시되지 않습니다. gcc의 어떤 버전을 사용하고 있습니까? –
어리석은 질문 일지 모르지만 오류 메시지에'virtual float airtime (std :: vector freq_bins)'이 두 번, Network.h 49 번, 54 번에 두 번 선언됩니다. 정확하게 클래스 정의? 그리고 어떤 라인 49, 53, 54 라인입니까? –
hvd
@hvd :'class network'에서'virtual float airtime (std :: vector freq_bins)'을 정의하는 줄을 복사 할 때 게시 된 오류가 발생합니다. 나는 네가 옳다고 생각한다. –