繁体   English   中英

无法重载虚拟功能

[英]unable to overload virtual function

我有一个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;

失败的原因是您的定义不正确,即使您指定应返回float,它们也不会返回任何内容。

提示:您确实不应该那样暴露类的内部状态。 在封装上进行一些谷歌搜索。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM