无法超载虚拟功能

时间:2011-12-17 21:45:48

标签: c++ virtual overloading

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

有没有人有任何见解?

1 个答案:

答案 0 :(得分:5)

尝试纯虚函数声明。

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

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

提示:你真的不应该像那样公开你的类的内部状态。做一些关于封装的谷歌搜索。