gny*_*his 1 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) { };
};
Run Code Online (Sandbox Code Playgroud)
现在,我有一个我想要重载它们的第二个类.这是这个类的标题:
#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
Run Code Online (Sandbox Code Playgroud)
然后我在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;
}
Run Code Online (Sandbox Code Playgroud)
我收到了关于返回值的警告,这不是问题.但是在尝试编译时我得到的错误,我不明白:
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> >)’
Run Code Online (Sandbox Code Playgroud)
我创建了一个更简化的程序来尝试理解为什么我会收到此错误,但这个简化的程序有效:
#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);
}
Run Code Online (Sandbox Code Playgroud)
这里有人有任何见解吗?
尝试纯虚函数声明.
virtual float airtime() = 0;
virtual float airtime(std::vector<int> freq_bins) = 0;
Run Code Online (Sandbox Code Playgroud)
它失败的原因是你的定义不正确,即使你已经指定它应该返回float,它们也不会返回任何内容.
提示:你真的不应该像那样公开你的类的内部状态.在封装上做一些谷歌搜索.
| 归档时间: |
|
| 查看次数: |
3775 次 |
| 最近记录: |