【问题标题】:unable to overload virtual function无法重载虚函数
【发布时间】:2011-12-17 21:45:48
【问题描述】:

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

这里有人有什么见解吗?

【问题讨论】:

  • 我没有收到您所看到的错误。你用的是什么版本的 gcc?
  • 也许是一个愚蠢的问题,但错误消息说 virtual float airtime(std::vector&lt;int&gt; freq_bins) 被声明了两次,一次在 Network.h 第 49 行,一次在第 54 行。您能否再次检查您是否复制了类定义准确?第 49 行、第 53 行和第 54 行分别是哪几行?
  • @hvd:当我在class Network 中复制定义virtual float airtime(std::vector&lt;int&gt; freq_bins) 的行时,我收到了发布的错误。我想你一定是对的。

标签: c++ virtual overloading


【解决方案1】:

尝试纯虚函数声明。

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

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

提示:你真的不应该那样暴露你的类的内部状态。对封装进行一些谷歌搜索。

【讨论】:

  • 我可能很慢,缺少return语句只是一个警告(只要函数从不调用它是合法的),错误的原因是什么?
  • 这可能与您的编译器有关,并且您正在使用 -Wall。否则您的代码将编译... ideone.com/E9mzL
  • 我不确定我是否理解您的评论。需要明确的是,我没有得到错误,也没有看到错误的原因。发布的函数定义格式正确,必须由符合标准的编译器接受,尽管警告是适当的,因为如果调用它们,行为将是未定义的。
  • 好吧,我和你说的一样,发布的代码编译,如我提供的链接所示。这意味着问题必须出在 OPs 编译器或 Op 在问题中遗漏的东西。
  • 好的,我现在明白了。只是你在回答中说:“它失败的原因......”我以为你在指出编译错误的原因。
猜你喜欢
  • 2017-08-06
  • 2023-01-07
  • 2017-08-12
  • 2013-03-27
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 2012-02-07
相关资源
最近更新 更多