无法重载虚函数

unable to overload virtual function

本文关键字:函数 重载      更新时间:2023-10-16

我有一个Network类,我想重载两个虚函数: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);
}

有人有什么见解吗?

尝试纯虚函数声明。

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

失败的原因是你的定义不正确,它们不返回任何东西,即使你已经指定它应该返回浮点数。

提示:你真的不应该像那样暴露类的内部状态。在谷歌上搜索一下封装。