I've visited this forum many many many times, but this is my actual first post here. Usually I can find my answer here and I guess I've probably found it this time, but this time my knowledge is lacking to understand the solutions given (been learning C++ for the last 2 weeks).
The error I get:
no matching function for call to 'WidgetBridge::WidgetBridge()'
an extraction of my (rather lengthy) code:
class Room {
private:
//initializer list of internal objects
WidgetBridge bridge_thermostat;
WidgetBridge bridge_relay;
public:
//Constructor of the class:
Room() : bridge_thermostat(V100), bridge_relay(V107){}
void initBridges(String authThermostat, String authRelay){
bridge_thermostat.setAuthToken(authThermostat);
bridge_relay.setAuthToken(authRelay);
}
void receiveCurrentT(float param){
currentT = param;
Blynk.virtualWrite(V10, currentT);
timer.restartTimer(thermostatTimer ); //reset isDead timer for thermostat
Blynk.setProperty(V17, "color", BLYNK_GREEN); //change LED color
Blynk.virtualWrite(V17, 200);
}
} livingRoom;
BLYNK_CONNECTED() {
Blynk.syncAll();
livingRoom.initBridges("xxx", "xxxx"); //auth of: thermostat, relay
}
BLYNK_WRITE(V10){ livingRoom.receiveCurrentT(param.asFloat()); } //receive currentT from thermostat
Based on the answers I've found on this forum it appears that WidgetBridge doens't have its own constructor when called. Based on the answers given I've also tried:
public:
//Constructor of the class:
Room() : {
bridge_thermostat = V100;
bridge_relay = V107;
}
but that rendered the same error. I've continued reading about static fields, constructors, namespaces, etc. but bottomline: I'm stuck and I don't know how to fix this.
Additional info: code is for an esp8266 arduino wifi module which communicates with other esp8266's (relay and thermostat). The communication takes place through 'bridges' which are created using code from the Blynk app.
Thank you for your time!
UPDATE: I've finally found the actual calss widgetbridge itself. And from the mentioned solution I gathered that it has no constructor of itself, but it does..so now I'm really lost. Here's part of the widget class:
class WidgetBridge
: private BlynkWidgetBase
{
public:
WidgetBridge(uint8_t vPin)
: BlynkWidgetBase(vPin)
{}
void setAuthToken(const char* token) {
char mem[BLYNK_MAX_SENDBYTES];
BlynkParam cmd(mem, 0, sizeof(mem));
cmd.add(mPin);
cmd.add("i");
cmd.add(token);
Blynk.sendCmd(BLYNK_CMD_BRIDGE, 0, cmd.getBuffer(), cmd.getLength()-1);
}
(....)
};
From the code extract you posted (partial) and the error message(partial too...) , the only reasonable answer is that the WidgetBridge class as no default constructor (i.e. constructor with 0 argument).
Probably because the base class BlynkWidgetBase has no default constructor as well.
So you get compiler errors on those lines
//initializer list of internal objects
WidgetBridge bridge_thermostat;
WidgetBridge bridge_relay;
You can either implement a WidgetBride default constructor or instanciate those two variables with the constructor taking a uint8_t parameter :
//initializer list of internal objects
WidgetBridge bridge_thermostat(3);
WidgetBridge bridge_relay(4);
3 and 4 to be replaced by whatever value that makes sense, but only you can know that from the code extract
Related
I'm working on an assignment that uses OMNeT++, Veins/Sumo. Although my question is not completely related to it, it might add some context.
I'm familiar with programming, but having a little bit of trouble wrapping my head around the whole
The idea is that there is a network of cars, and all these cars are talking to eachother using messages. A message inlcudes the vehicle name, but also information about the current position/mobility of the car. This is where TraCIMobilitycomes in.
Ideally I would like to have all this information stored in the original class in the message, but I am running into some issues.
pointer/memory address idea when working with classes.
class InterVehicleMessage : public ::veins::DemoSafetyMessage
{
protected:
::omnetpp::opp_string vehicleName;
veins::TraCIMobility vehicle_mobility; <--- important
private:
void copy(const InterVehicleMessage& other);
protected:
bool operator==(const InterVehicleMessage&);
public:
//stuff
virtual veins::TraCIMobility& getMobility(); <--- important
virtual const veins::TraCIMobility& getMobility() const {return const_cast<InterVehicleMessage*>(this)->getMobility();} <--- important
virtual void setMobility(const veins::TraCIMobility& vehicle_mobility); <--- important
};
This all looks fine and dandy, I assume it functions as intended aswell.
But then when I try to make define the setMobility class I run into some problems.
void InterVehicleMessage::SetMobility(const veins::TraCIMobility& vehicle_mobility)
{
this->vehicle_mobility = vehicle_mobility;
}
This results in the error:
InterVehicleMessage_m.cc:240:28: error: object of type 'veins::TraCIMobility' cannot be assigned because its copy assignment operator is implicitly deleted
I'm not familiar with C++ enough to really know what this means. Is there anyone who could hint me into the right direction?
pastebin to TraCIMobility.h: https://pastebin.com/pGZEepxX
I've decided to save the information necesarry from TraCIMobility in individual variables.
So:
double speed;
veins::Coord position;
veins::Heading direction;
veins::Coord nextPositions[7];
Instead of:
veins::TraCIMobility vehicle_mobility;
As #PaulSanders stated in the comment, this class is not supposed to be copied.
Thanks everyone for their time and effort. Have a nice day.
I didn't know that I didn't know this :) . and a similar question here didn't help much.
So here i am asking. Please Consider the following class:
//in Agent.h
class Agent : public ns3::Object{
private:
//...
static BaseWifi m_wifi;
//...
};
is this :
//Agent.cpp
BaseWifi temp;
BaseWifi Agent::m_wifi = temp;
very much different from this:
//Agent.cpp
BaseWifi Agent::m_wifi = BaseWifi();
The second approach doesn't work for me.
why and how?
I don't want to trouble you with more codes coz I faced this problem deep in my program. The program generate seg faults as things(members) inside BaseWifi's constructor are not initialized correctly. when those uninitialized internal members are used, seg faults occur.
Thank you in advance for your kind comments and answers.
p.s.:
In fact I found this issue when I hadn't yet initialized this static member and I was deleting an extra line :
BaseWifi temp;
in my main(), which added more to my confusion!!!(this one could be dependent on what I put in BaseWifi's constructor, so dont mind it for now)
Update-1:
For those who would like to see the BaseWifi:
class BaseWifi {
ns3::WifiHelper m_wifiHelper; // a wifi helper apply to setup vehicles Wifi
ns3::NqosWifiMacHelper m_wifiMacHelper; // a wifi mac helper apply to setup vehicles Wifi
ns3::YansWifiPhyHelper m_wifiPhyHelper; // a wifi phy helper apply to setup vehicles Wifi
std::string m_phyMode;
double m_rss; // -dBm
bool m_init_done;
public:
BaseWifi();
virtual void init();
ns3::NetDeviceContainer Install(ns3::NodeContainer &c);
virtual ~BaseWifi();
};
BaseWifi::BaseWifi() {
m_init_done = false;
m_rss = -80;
m_phyMode ="DsssRate1Mbps";
// TODO Auto-generated constructor stub
init();
}
void BaseWifi::init() {
NS_LOG_UNCOND("inside BaseWifi::init()");
m_wifiHelper.SetStandard (ns3::WIFI_PHY_STANDARD_80211b);
m_wifiPhyHelper = ns3::YansWifiPhyHelper::Default ();
// This is one parameter that matters when using FixedRssLossModel
// set it to zero; otherwise, gain will be added
m_wifiPhyHelper.Set ("RxGain", ns3::DoubleValue (0) );
// ns-3 supports RadioTap and Prism tracing extensions for 802.11b
m_wifiPhyHelper.SetPcapDataLinkType (ns3::YansWifiPhyHelper::DLT_IEEE802_11_RADIO);
ns3::YansWifiChannelHelper wifiChannel;
wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
// The below FixedRssLossModel will cause the rss to be fixed regardless
// of the distance between the two stations, and the transmit power
wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",ns3::DoubleValue (m_rss));
m_wifiPhyHelper.SetChannel (wifiChannel.Create ());
// Add a non-QoS upper mac, and disable rate control
m_wifiMacHelper = ns3::NqosWifiMacHelper::Default ();
m_wifiHelper.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode",ns3::StringValue (m_phyMode),
"ControlMode",ns3::StringValue (m_phyMode));
// Set it to adhoc mode
m_wifiMacHelper.SetType ("ns3::AdhocWifiMac");
m_init_done = true;
}
//Install the class's embedded settings on the nodes in this node container.
ns3::NetDeviceContainer BaseWifi::Install(ns3::NodeContainer &nc) {
return m_wifiHelper.Install(m_wifiPhyHelper, m_wifiMacHelper, nc);
}
I have run into these kind of issues before. Apparently the initialization of static member objects very much depends on where the implementation is done in your code and (probably) on how the whole thing is compiled. The solution that I found (somewhere) to the problem was to wrap the whole thing into a static member function like this:
//in Agent.h
class Agent : public ns3::Object{
private:
//...
static BaseWifi& m_wifi();
//...
};
and:
//in Agent.cpp
BaseWifi& Agent::m_wifi() {
static BaseWifi TheObject=BaseWifi();
return TheObject;
}
This way the object is initialized properly the first time the static member function is called.
The difference here is the first approach is using copy constructor to initialize the object while the second is using the default constructor.
I am trying to iron out my first compile in a long time, and I think this is the last hurdle.
I am trying to create this type of hierarchy in my design.
class chin_
{
private:
charon_* engine;
public:
chin_();
chin_(charon_ &handle)
{
engine = handle;
}
~chin_();
};
//////////////
class charon_ {
private:
chin_ engine_input;
public:
charon_(){
engine_input = chin_(this);
}
~charon_();
};
I am getting errors that tell me there is no matching function for call to
'chio::chin_::chin()'
it also says, mind you this is only when I change the constructor to charon_&
public:
chin_(const charon_ &handle)
{
engine = handle;
}
sys/../headers/chio.h:104:5: note: no known conversion for argument 1 from ‘charon::charon_* const’ to ‘const charon::charon_&’
When I use the * instead of & I get this instead, which is by far more baffling.
sys/../headers/chio.h:104:5: note: chio::chin_::chin_(const charon::charon_*)
sys/../headers/chio.h:104:5: note: candidate expects 1 argument, 0 provided
I've tried this and that, and I figure it is probably something super simple. I've tried to match my code up squarely with the examples I've found though so I don't know what else I should be trying, I'd like to be able to figure this sort of stuff out on my own. I just am not having any luck with it.
EDIT:
I update the code but I left the updated error for the bottom.
sys/chin.cpp:19:14: error: cannot convert ‘charon::charon_’ to ‘charon::charon_*’ in assignment
It takes me to this code
chin_::chin_(charon_ &handle) {
engine = handle;
}
engine is defined as
charon_* engine; and handle appears to be an address and should fall right into place, but it isn't. I think I am not using this correctly.
When you declare something like this :
chin_ engine_input;
chin_ needs a constructor with no parameters or you have to pass the parameters to it in the initializer list, otherwise you will get the error about no matching constructor.
What you can do is either have a default constructor that takes no params. Or make engine_input a pointer and create it with :
engine_input = new chin_(handle);
Alternatively you can pass handle to chin_ in the initializer list like this :
foo::foo(charon* handle):
chin_(handle)
{
}
Here is what i am trying to do.
I have Three Classes:
1) CEngine
2) CLogManager
3) CWindowGL
Ad1.
This class 'does' the tricky things to get the game engine going,
an application utilizing it, can call only few public members to
get the game going -
class CEngine
{
public:
CEngine();
~CEngine(); // should this go to private?
bool Init(width,height,...);
void Destroy();
void Run();
bool LoadMap(...);
private:
CLogManager *m_pLogManager;
CWindowGL *m_pWindowManager
}
// Example usage
CEngine *Engine=new CEngine;
Engine->Initialize(...)
Engine->LoadMap(...)
Engine->Run()
Engine->Destroy()
delete(Engine)
Ad2.
This class controls the logging facility
it just allows me to dump some log into the log data file:
class CLogManager
{
public:
CLogManager();
~CLogManager();
void Write(const char *fmt,...);
private:
FILE *fp;
std::string m_sFileName; // unique filename generated at the constructor
SYSTEMTIME m_tSystemTime;
}
Ad3.
This class handles the window creation, and pixel format settings,
and few other things related to the window itself - nothing else,
but it also needs to utilize CLogManager - to dump few informations
for debug purposes.
Now the question is:
When a CLogManager constructor is called, class generates a unique filename that is:
m_sFileName="data/logs/enginelog_%i%i%i.txt"; // hour, minute, second
CEngine class in the Init method does:
m_pLogManager = new CLogManager;
and later on it uses it with m_pLogManager->Write(....) to log events.
That's ok for CEngine, but i would like to use the same functionality
in CWindowGL class and here is the question.
I would like my code to share CLogManager across :
CEngine
CWindowGL
...
...
...
and few others that i'll implement.
I can't do this by adding "Instance()" type of method like:
static CLogManager &Instance()
{
static CLogManager s_instance;
return s_instance;
}
and calling:
CLogManager::Instance().Write(" LOG MESSAGE ");
As this would cause my CLogManager to generate new filename each time when a
constructor is called.
Do i have to
extern CEngine *Engine;
somewhere to call
Engine->Log(" LOG MESSAGE ")
wrapper everytime or there is something else i can stick to?
I know it is more like a 'code-design' question, but i would like to see
how do you guys handle such things.
Normally i would do this with extern, but that would require me to check
m_pLogManager!=NULL within a wrapper function to a private member - and just
don't know if that's OK.
Maybe there's some other - better approach?
I will be adding few other classes like. TexturesManager - and would like this class to
store the actual size of textures loaded and so on, so this would also require me to
not to call Instance() to class each time the texture is called - as this would create/destruct the class without storing the needed size / array of textures already loaded...
Uff..
Thanks, hope this is clear.
I can't do this by adding "Instance()" type of method as this would cause my CLogManager to generate new filename each time when a constructor is called.
Actually no, the constructor would be called only once during your program lifetime. The singleton pattern is what you most likely want for your logging class.
What you'll generally find in these situations is a static set of methods that use a singleton underneath. All consumers call the static method which returns the one, single, instance of your logger, which you then call methods on.
So I've got a Piece class supposed to represent pieces on a board, from which I plan to have two other classes inheriting. I've gotten a lot of problems in doing so, however; here is the relevant code so far.
///
/// PIECE CLASS HERE
/// this is an abstract class from which Barrier and Pawn inherit.
class Piece
{
public:
Piece(Space* start);
sf::Shape m_Circle;
protected:
int m_X;
int m_Y;
int m_radius;
Space* CurrentSpace;
};
Piece::Piece(Space* start):
m_X(start->GetX()),
m_Y(start->GetY()),
m_radius(14),
CurrentSpace(start)
{}
///
/// BARRIER CLASS HERE
/// these are the white stones that block a player's path
class Barrier : public Piece
{
public:
Barrier(Space* initial);
void Move(Space* target, bool isCapturing);
};
Barrier::Barrier(Space* initial)
{
Piece(initial);
m_Circle = sf::Shape::Circle((float)m_X, (float)m_Y, (float)m_radius, sf::Color(255, 255, 255));
Move(initial);
}
void Barrier::Move(Space* target, bool isCapturing)
{
int xChange = abs(target->GetX() - m_X);
int yChange = abs(target->GetY() - m_Y);
m_Circle.Move((float)xChange, (float)yChange);
CurrentSpace.ToggleOccupied();
if(!isCapturing)
{
(*target).ToggleOccupied();
}
CurrentSpace = target;
}
I'm getting loads of errors I don't understand, in particular:
no matching function for call to Piece::Piece()
declaration of 'Piece initial' shadows a parameter
no matching function for call to 'Barrier::Move(Piece&)'
request for member 'ToggleOccupied' in '((Barrier*)this)->Barrier::<anonymous>.Piece::CurrentSpace', which is of non-class type 'Space*'|
Being new to C++, I don't understand what's going wrong with any of this. I tried to build my code analogous to the code I found in the book I used to learn C++, but apparently I've overlooked some subtlety. All the functions that I try to call seem to exist in the appropriate places, and I define them with the same values as in their prototype, I think.
The first error is caused by this:
Barrier::Barrier(Space* initial)
{
Piece(initial);
m_Circle = sf::Shape::Circle((float)m_X, (float)m_Y, (float)m_radius, sf::Color(255, 255, 255));
Move(initial);
}
Needs to look like:
Barrier::Barrier(Space* initial) : Piece(initial)
{
m_Circle = sf::Shape::Circle((float)m_X, (float)m_Y, (float)m_radius, sf::Color(255, 255, 255));
Move(initial);
}
The base class' constructor runs before your constructor (no matter what) -- you need to pass arguments to it if it requires arguments in the initialization list.
I'm not sure about the other errors because I don't know which lines they're occurring on.
The parenthesis in the line
Piece(initial);
are ignored by the compiler. You are declaring a variable with the same name as the parameter:
Piece initial;
To initialize the Piece base object with initial, you have to use the member initializer list:
Barrier::Barrier(Space* initial) : Piece(initial)
Also, the Move function expects two arguments, but you're only passing one. You forgot the boolean.
Good answers, all. Plus that final daunting line
request for member 'ToggleOccupied' in '((Barrier*)this)->Barrier::<anonymous>.Piece::CurrentSpace', which is of non-class type 'Space*'|
is being caused from
CurrentSpace.ToggleOccupied();
you have declared CurrentSpace to be a pointer, so this should be
CurrentSpace->ToggleOccupied();
If you want to initialize the super class, you should do it like this:
Barrier(Space* initial):
Piece(initial) {
...
}
If you don't explicitly initialize the base type, the compiler will try to initialize it by calling a constructor with an empty argument list, like this:
Barrier(Space* initial):
Piece() {
...
}
But since you don't define a constructor for Piece that takes zero arguments, you get the compiler error you described.