I have a Network class, which I want two virtual functions which I am going to overload: airtime() and airtime(std::vector<int> freq_bins);
I define the class, and the functions at the bottom:
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) { };
};
Now, I have a second class which I want to overload them. Here is the header of this class:
#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
I then define them in 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;
}
I get the warning on returning values, that's not a problem. But the errors I get when trying to compile this, I don't understand:
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> >)’
I created a more simplified program to try and understand why I get this error, yet this simplified program works:
#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);
}
Does anyone have any insight here?
Try pure virtual function declarations.
virtual float airtime() = 0;
virtual float airtime(std::vector<int> freq_bins) = 0;
The reason it fails is that you definitions are incorrect, they don't return anything even though you have specified that it should return float.
Tip: You really shouldn't expose the internal state of your class like that. Do some googling on encapsulation.
Related
I'm dealing with an issue right now in C++, where I have a defined variable existing within my class definition, the compiler gets angry and tells me that said variable has not been defined within the scope, and that the constructor doesn't have any field for the variable in question.
I have a simple class hierarchy where I have the base class, Neuron, and two subclasses HiddenNeuron and OutputNeuron, that are both inheriting from Neuron in public virtual mode.
I've made a neuron.h file where I defined the virtual header class, and as such, I have been told by my instructor that I need not write an implementation file for it.
I've then written two header files for HiddenNeuron and OutputNeuron, in which I define them, and two corresponding cpp files that implement them, respectively.
When I try running
g++ neuralnet.cpp input_neuron.cpp hidden_neuron.cpp output_neuron.cpp
I get a bunch of errors like:
hidden_neuron.cpp: In member function ‘virtual void HiddenNeuron::input(long double)’:
hidden_neuron.cpp:10:52: error: ‘value’ was not declared in this scope
inline void HiddenNeuron::input(double long val) { value = val; }
and
hidden_neuron.cpp: In member function ‘virtual void HiddenNeuron::input(long double)’:
hidden_neuron.cpp:10:52: error: ‘value’ was not declared in this scope
inline void HiddenNeuron::input(double long val) { value = val; }
Here's the code for my classes:
neuron.h:
#ifndef NEURON_H
#define NEURON_H
class Neuron {
//protected:
// double long value;
public:
Neuron(void);
//Neuron(const double long val);
//
virtual double long activation(void) const =0;
virtual void input(double long val);
};
#endif
output_neuron.h:
#ifndef OUTPUT_NEURON_H
#define OUTPUT_NEURON_H
#include "neuron.h"
class OutputNeuron : public virtual Neuron {
private:
double long value;
public:
OutputNeuron(void);
OutputNeuron(const double long val);
// this should return a value between 0 & 1
virtual double long activation(void) const;
// vector multiplication of the weight vector
// and all the values of the last layer
// with an additional corresponding bias
// added onto the end.
virtual void input(double long val);
};
#endif
output_neuron.cpp:
#include "output_neuron.h"
#include <cmath>
OutputNeuron::OutputNeuron(void) : value(0) { }
OutputNeuron::OutputNeuron(const double long val)
: value(val) { }
inline double long OutputNeuron::activation(void) const {
return 1 / (1 + exp((-1)*value));
}
inline void OutputNeuron::input(double long val) {
value = val;
}
hidden_neuron.h:
#ifndef HIDDEN_NEURON_H
#define HIDDEN_NEURON_H
#include "neuron.h"
class HiddenNeuron : public virtual Neuron {
private:
double long value;
public:
HiddenNeuron(void);
HiddenNeuron(const double long val);
// activation function that takes the stored value
// and squashes it and returns it
virtual double long activation(void) const;
virtual void input(double long val);
};
#endif
hidden_neuron.cpp:
#include "hidden_neuron.h"
#include <cstdlib>
#include <cmath>
HiddenNeuron::HiddenNeuron(void)
: value(pow(-1, rand()) * static_cast<double>(rand())/static_cast<double>(RAND_MAX)){}
HiddenNeuron::HiddenNeuron(const double long val) : value(val) { }
inline void HiddenNeuron::input(double long val) { value = val; }
inline double long HiddenNeuron::activation(void) const {
return 1/(1 + exp((-1)*value));
}
FULL ERROR LOG:
neuralnet.cpp: In member function ‘void NeuralNet::initialize(bool)’:
neuralnet.cpp:72:18: error: invalid new-expression of abstract class type ‘HiddenNeuron’
temp = new HiddenNeuron;
^~~~~~~~~~~~
In file included from neuralnet.h:5:0,
from neuralnet.cpp:1:
hidden_neuron.h:5:7: note: because the following virtual functions are pure within ‘HiddenNeuron’:
class HiddenNeuron : public virtual Neuron {
^~~~~~~~~~~~
In file included from hidden_neuron.h:3:0,
from neuralnet.h:5,
from neuralnet.cpp:1:
neuron.h:14:16: note: virtual void Neuron::input(long double) const
virtual void input(double long val) const =0;
^~~~~
neuralnet.cpp:85:18: error: invalid new-expression of abstract class type ‘HiddenNeuron’
temp = new HiddenNeuron;
^~~~~~~~~~~~
neuralnet.cpp:98:18: error: invalid new-expression of abstract class type ‘OutputNeuron’
temp = new OutputNeuron;
^~~~~~~~~~~~
In file included from neuralnet.h:7:0,
from neuralnet.cpp:1:
output_neuron.h:5:7: note: because the following virtual functions are pure within ‘OutputNeuron’:
class OutputNeuron : public virtual Neuron {
^~~~~~~~~~~~
In file included from hidden_neuron.h:3:0,
from neuralnet.h:5,
from neuralnet.cpp:1:
neuron.h:14:16: note: virtual void Neuron::input(long double) const
virtual void input(double long val) const =0;
^~~~~
hidden_neuron.cpp: In constructor ‘HiddenNeuron::HiddenNeuron()’:
hidden_neuron.cpp:6:5: error: class ‘HiddenNeuron’ does not have any field named ‘value’
: value(pow(-1, rand()) * static_cast<double>(rand())/static_cast<double>(RAND_MAX)){}
^~~~~
hidden_neuron.cpp: In constructor ‘HiddenNeuron::HiddenNeuron(long double)’:
hidden_neuron.cpp:8:53: error: class ‘HiddenNeuron’ does not have any field named ‘value’
HiddenNeuron::HiddenNeuron(const double long val) : value(val) { }
^~~~~
hidden_neuron.cpp: In member function ‘virtual void HiddenNeuron::input(long double)’:
hidden_neuron.cpp:10:52: error: ‘value’ was not declared in this scope
inline void HiddenNeuron::input(double long val) { value = val; }
^~~~~
hidden_neuron.cpp:10:52: note: suggested alternative: ‘val’
inline void HiddenNeuron::input(double long val) { value = val; }
^~~~~
val
hidden_neuron.cpp: In member function ‘virtual long double HiddenNeuron::activation() const’:
hidden_neuron.cpp:13:26: error: ‘value’ was not declared in this scope
return 1/(1 + exp((-1)*value));
^~~~~
hidden_neuron.cpp:13:26: note: suggested alternative: ‘valloc’
return 1/(1 + exp((-1)*value));
^~~~~
valloc
output_neuron.cpp: In constructor ‘OutputNeuron::OutputNeuron()’:
output_neuron.cpp:4:36: error: class ‘OutputNeuron’ does not have any field named ‘value’
OutputNeuron::OutputNeuron(void) : value(0) { }
^~~~~
output_neuron.cpp: In constructor ‘OutputNeuron::OutputNeuron(long double)’:
output_neuron.cpp:7:5: error: class ‘OutputNeuron’ does not have any field named ‘value’
: value(val) { }
^~~~~
output_neuron.cpp: In member function ‘virtual long double OutputNeuron::activation() const’:
output_neuron.cpp:10:28: error: ‘value’ was not declared in this scope
return 1 / (1 + exp((-1)*value));
^~~~~
output_neuron.cpp:10:28: note: suggested alternative: ‘valloc’
return 1 / (1 + exp((-1)*value));
^~~~~
valloc
output_neuron.cpp: In member function ‘virtual void OutputNeuron::input(long double)’:
output_neuron.cpp:14:3: error: ‘value’ was not declared in this scope
value = val;
^~~~~
output_neuron.cpp:14:3: note: suggested alternative: ‘val’
value = val;
^~~~~
val
If anyone can help me, I've been pulling my hair out with this issue.
Edit: using class design as it's a requirement for the course I'm currently enrolled in.
Edit 2: included the full error log
Edit 3: Okay so this is really bizarre, what I wrote works in a separate project directory as pointed out by user idontseethepoint, however when it's in the same directory as my main project, it still gives me the exact same error.
I do understand why the following would be a problem if no namespaces were used. The call would be ambiguous indeed. I thought "using stD::swap;" would define which method to use.
Why does it work for "int" but not a "class"?
#include <memory>
namespace TEST {
class Dummy{};
void swap(Dummy a){};
void sw(int x){};
}
namespace stD {
void swap(TEST::Dummy a){};
void sw(int x){};
class aClass{
public:
void F()
{
using stD::swap;
TEST::Dummy x;
swap(x);
}
void I()
{
using stD::sw;
int b = 0;
sw(b);
}
};
}
This is the error message:
../src/Test.h: In member function ‘void stD::aClass::F()’:
../src/Test.h:26:9: error: call of overloaded ‘swap(TEST::Dummy&)’ is ambiguous
swap(x);
^
../src/Test.h:26:9: note: candidates are:
../src/Test.h:17:6: note: void stD::swap(TEST::Dummy)
void swap(TEST::Dummy a){};
^
../src/Test.h:10:6: note: void TEST::swap(TEST::Dummy)
void swap(Dummy a){};
^
I thank you very much in advance for an answer.
This line is using argument dependent lookup
TEST::Dummy x;
swap(x);
So it will find both void stD::swap(TEST::Dummy) as well as void TEST::swap(TEST::Dummy) because x carries the TEST:: namespace.
In the latter case int b = 0; the variable b is not in a namespace, so the only valid function to call would be stD::sw due to your using statement.
abstract base class:
#ifndef BUILDINGORG_H
#define BUILDINGORG_H
#include <iostream>
#include <memory>
#include <vector>
class BuildingOrg
{
public:
BuildingOrg(int _id);
virtual int addBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg,
std::string _type) const;
virtual void removeBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg);
virtual void getInfo()=0;
private:
int id;
std::string type;
};
#endif // BUILDINGORG_H
concrete subclass:
#ifndef BUILDINGCOMPONENT_H
#define BUILDINGCOMPONENT_H
#include "buildingorg.h"
class BuildingComponent : public BuildingOrg
{
public:
BuildingComponent(int _id);
int addBuildingComponent(std::shared_ptr<BuildingOrg> _buildingOrg,
std::string _type) const override;
void removeBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg)
override;
void getInfo() override;
private:
std::vector<std::shared_ptr<BuildingOrg>> building_Org;
};
#endif // BUILDINGCOMPONENT_H
Implementation of subclass:
#include "buildingcomponent.h"
BuildingComponent::BuildingComponent(int _id):
BuildingOrg(_id)
{
}
int BuildingComponent::addBuildingComponent(std::shared_ptr<BuildingOrg> _buildingOrg, std::string _type) const
{
building_Org.push_back(_buildingOrg);// I am having error here
return 1;
}
void BuildingComponent::removeBuildingComponent(std::shared_ptr<BuildingOrg> buildingOrg)
{
}
void BuildingComponent::getInfo()
{
}
When I try to put shared pointer in my Vector I get this nasty error;
I really don't know why I am getting the error:
cpp:10: error: passing 'const std::vector<std::shared_ptr<BuildingOrg> >' as 'this' argument of 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::shared_ptr<BuildingOrg>; _Alloc = std::allocator<std::shared_ptr<BuildingOrg> >; std::vector<_Tp, _Alloc>::value_type = std::shared_ptr<BuildingOrg>]' discards qualifiers [-fpermissive]
building_Org.push_back(_buildingOrg);
I don’t understand what is it saying.
The const in int addBuildingComponent(std::shared_ptr<BuildingOrg> _buildingOrg, std::string _type) const override; is a promise that addBuildingComponent will not change BuildingComponent. However, it tries to modify the member variable building_Org with the push_back()...
Removing the const from addBuildingComponent() should fix the error.
The discards qualifiers part of the error message refers to the conflict with the const qualifier of the member function.
C++ template related error messages can be notoriously difficult to parse at first, but it does get easier with practice :-)
You defined BuildingComponent::addBuildingComponent method as const (i.e. that it won't change member varialbles), but you are adding passed in value to a member list (i.e. changing the member variable).
addBuildingComponent() is a const method. within its scope, *this is const, and so this->building_Org is const.
std::vector::push_back() is a non-const method. So it can't be called in a context where the vector is const.
Error:
C:\testa\game.cpp|147|error: no matching function for call to 'game::register_handler(PacketFamily, PacketAction,
<unresolved overloaded function type>)'|
Heres part of the code since it's too big.
I had to typedef handler_callback inside class game otherwise one of the two would be undefined for the other.
game.h
class game
{
typedef bool (game::*handler_callback)(PacketReader reader);
public:
bool default_handler_init (PacketReader reader);
void register_default_handlers();
void register_handler(PacketFamily family, PacketAction action,handler_callback callback);
};
game.cpp
void game::register_default_handlers()
{
register_handler(PACKET_F_INIT, PACKET_A_INIT,default_handler_init);
}
void game::register_handler(PacketFamily family, PacketAction action,handler_callback callback)
{
handlers.insert(std::make_pair(std::make_pair(family, action), callback));
}
I expanded the OP's code to the following so that it would compile:
class PacketReader {};
class PacketFamily {};
class PacketAction {};
const PacketFamily PACKET_F_INIT;
const PacketAction PACKET_A_INIT;
class game
{
typedef bool (game::*handler_callback)(PacketReader reader);
public:
bool default_handler_init (PacketReader reader);
void register_default_handlers();
void register_handler(PacketFamily family, PacketAction action, handler_callback callback);
};
void game::register_default_handlers()
{
register_handler(PACKET_F_INIT, PACKET_A_INIT, default_handler_init);
}
void game::register_handler(PacketFamily family, PacketAction action, handler_callback callback)
{
//handlers.insert(std::make_pair(std::make_pair(family, action), callback));
}
Compiling with g++ using the command g++ -c -Wall -std=c++11 test.cpp yields the following errors:
test.cpp: In member function ‘void game::register_default_handlers()’:
test.cpp:18:71: error: no matching function for call to ‘game::register_handler(const PacketFamily&, const PacketAction&, <unresolved overloaded function type>)’
register_handler(PACKET_F_INIT, PACKET_A_INIT,default_handler_init);
^
test.cpp:18:71: note: candidate is:
test.cpp:13:10: note: void game::register_handler(PacketFamily, PacketAction, game::handler_callback)
void register_handler(PacketFamily family, PacketAction action,handler_callback callback);
^
test.cpp:13:10: note: no known conversion for argument 3 from ‘<unresolved overloaded function type>’ to ‘game::handler_callback {aka bool (game::*)(PacketReader)}’
Anyway, to fix the problem, add &game:: to the call to register_handler:
register_handler(PACKET_F_INIT, PACKET_A_INIT,&game::default_handler_init);
I´ve got this CRac class and it´s giving me some problems with its members.
//Definition TAD CRac.hpp
#ifndef CRAC_H
#define CRAC_H
namespace bblRac{
struct Racional{
int num, denom;
};
class CRac{
public:
CRac();
void read();
void asignarVal (const CRac& otroRac);
void write();
void add(const CRac& otroRac)const;
private:
Racional rac;
void simplif();
}; //End of class CRac
} //End of namespace bblrac
#endif
In a ccp file I have
#include "CRac.hpp"
using namespace bblRac;
void CRac::add(const CRac& otroRac)const{
CRac res;
res.num= rac.num + otroRac.num; //line 98
res.denom= rac.denom + otroRac.denom;
}
And when I run it, the output is
CRac.cpp:98: error: ‘class bblRac::CRac’ has no member named ‘num’
CRac.cpp:98: error: ‘const class bblRac::CRac’ has no member named ‘num’
CRac.cpp:99: error: ‘class bblRac::CRac’ has no member named ‘denom’
CRac.cpp:99: error: ‘const class bblRac::CRac’ has no member named ‘denom’
I have tried to fix it with the pointer this, but it continues giving the same mistake..
Thank you!
This will fix it.
#include "CRac.hpp"
using namespace bblRac;
void CRac::add(const CRac& otroRac)const{
CRac res;
res.rac.num= rac.num + otroRac.rac.num; //line 98
res.rac.denom= rac.denom + otroRac.rac.denom;
}
Your CRac class contains a Racional member variable, num and denom are not members of CRac.
res.rac.num = whatever;
otroRac.rac.num = whatever;
Please look your code over before you hit copy/paste into StackOverflow.