No Matching function to call C++ - c++

I have 2 classes TreeManager and TreeProducerBase. I am getting an error in passing an object of TTree( which is a class to make trees) to a function in TreeProducerBase from TreeManagerconstructor.
Note : I have defined tree as
TTree *tree_
Function call :
tpb.initialize(&tree_);
Here, tpb is an object of TreeproducerBase class.
This is the function that is being called.
void initialize(TTree &tree_)
It shows the error as follows:
error: no matching function for call to 'TreeProducerBase::initialize(TTree*&)'
Where am I doing wrong?

You are trying to pass pointer-to-pointer-to-TTree to a function that expects reference-to-TTree. Try redeclare it like
void initialize(TTree* &tree_);
Invokation will look like
tpb.initialize(tree_);
And then you can initialize outer pointer via simple assignment:
void initialize(TTree* &tree_) {
tree_ = new TTree(); // or smth else
}

Related

how to deal with a function pointer problem?

i'm implementing a normal function pointer.
so this is the function that i want to call:
WndDyn* Punkt2d::pEditPunkt(WndInfo& wi, Int32 AnzSichtChar, Bool WithUnit,
const DecimalsConf& DecConf)
{
WynDyn_callback Dyncallback;
Dyncallback.AnzSichtChar = AnzSichtChar;
Dyncallback.WithUnit = WithUnit;
Dyncallback.DecConf = DecConf;
return &(DlgZeile(wi)
+ pEditAll(Dyncallback, &pEditFeldX)//pEditFeldX(AnzSichtChar, WithUnit,
DecConf)
+ FntXUnit(2)
+ pEditFeldY(AnzSichtChar, WithUnit, DecConf)
);
}
After defining the function that needs to be called i defined my callee as follow:
WndDyn* pEditAll(WynDyn_callback& Dyncallback, WndDyn* (func_Call)
(WynDyn_callback)) {
return func_Call(Dyncallback);
}
And last of all this is the function that needs to be called using the callee function:
WndDyn* Punkt2d::pEditFeldX(WynDyn_callback Dyncallback) {
return &Edit(pNewStrDataLink(m_x, DLUC_Length, Dyncallback.DecConf),
Dyncallback.AnzSichtChar)
.WithSelAllOnFocus(True);
}
My actuall problem is that my compiler is underlining the function pEditFeldX
in this line pEditAll(Dyncallback, pEditFeldX) in the function pEditpunkt and showing me this Error:
Severity Code Description Project File Line Suppression State
Error C3867 'Punkt2d::pEditFeldX': non-standard syntax; use '&' to
create a pointer to member
Severity Code Description Project File Line Suppression State Error
(active) E0167 argument of type "WndDyn (Punkt2d::)(WynDyn_callback
Dyncallback)" is incompatible with parameter of type "WndDyn
()(WynDyn_callback)"
Because pEditFeldX is a member function you can't just call pEditFeldX(Dyncallback). You must call the function on some Punkt2d object, using e.g. meinPunkt2d.pEditFeldX(Dyncallback).
If you write pEditFeldX(Dyncallback) inside the Punkt2d class then it means (*this).pEditFeldX(Dyncallback). The compiler adds (*this). to save some typing.
A function pointer only points to a function. It doesn't point to a function and an object. It points to pEditFeldX, not meinPunkt2d.pEditFeldX. You must specify the Punkt2d object when you call it.
To remember that a Punkt2d must be specified, a function pointer which points to a member function is declared as this: WndDyn* (Punkt2d::*func_Call)(WynDyn_callback) and called as this: meinPunkt2d.*func_Call(Dyncallback);
If the function pointer is &pEditFeldX then meinPunkt2d.*func_Call(Dyncallback) is the same as meinPunkt2d.pEditFeldX(Dyncallback)
This doesn't apply to static member functions. Static member functions can be used with normal function pointers since no object is required.
It is not quite clear what you are trying to do, but if I understand it right, I think that std::function would be able to solve your problem std::function is able to store anything which can be called, including "half of a function call" like you seem to want. std::bind can make these "half function calls".
You could use them like this:
// in pEditPunkt
pEditAll(Dyncallback, std::bind(&CPunkt2d::pEditFeldX, this, std::placeholders::_1))
// in pEditAll
WndDyn* pEditAll(WynDyn_callback& Dyncallback, std::function<WndDyn* (WynDyn_callback)> (func_Call)
(WynDyn_callback)) {
return func_Call(Dyncallback);
}
First of all i would like to thanks #user253751 for his patient and great support. i would like to share with you how my code look like now:
#include <functional>
// pointer function
WndDyn* pEditAll(WynDyn_callback& Dyncallback, std::function<WndDyn*
(K_WynDyn_callback)>func_Call) {
return func_Call(Dyncallback);
}
//the calle
WndDyn* K_Punkt2d::pEditPunkt( WndInfo& wi, Int32 AnzSichtChar,
Bool WithUnit, const DecimalsConf& DecConf)
{
WynDyn_callback Dyncallback;
Dyncallback.AnzSichtChar = AnzSichtChar;
Dyncallback.WithUnit = WithUnit;
Dyncallback.DecConf = DecConf;
return &(DlgZeile(wi)
+ pEditAll(Dyncallback,
std::bind(&Punkt2d::pEditFeldX, this,
std::placeholders::_1))//pEditFeldX(AnzSichtChar, WithUnit,
DecConf)
+ FntXUnit(2)
+ pEditFeldY(AnzSichtChar, WithUnit, DecConf)
);
}

Calling a vector of function pointer

I am trying to use vector of function pointer in C++ which belongs to one class and being called in another class. For example: In class BoundaryCondition (file BoundaryCondition.h), I initialize the function pointer as:
class BoundaryConditions{
public:
// Constructor function
BoundaryConditions(int id = 0);
vector <void (BoundaryConditions::*) (Face &, int, int)> func2bcond;
void fixedValue(Face &face, int id, int rkStep);
// and other functions with similar definition as fixedValue
}
In another class (DG.h), I initialize an array of BoundaryCondition instance, something like
BoundaryCondition *bcond;
and assign memory to bcond variable using new (in file DG.cpp). For each bcond[i] instance, I need to assign memory to function pointer as:
this->bcond[i].func2bcond.resize(totNoOfVariable);
I am using resize instead of pushback as file reading may not be in the required order. Next, I am assigning function to this function pointer (again in DG.cpp) as per my boundary condition file:
bcond[i].func2bcond[j] = (&BoundaryConditions::fixedValue);
The code compiles alright till this point. I am getting error when I try to call these functions. I call this in DG.cpp. The code is as follows:
(bcond[i].*func2bond[j])(f,1,2);
I keep getting the following error:
error: 'func2bcond' was not declared in this scope
I am quite sure it is only matter of position of * or the brackets, but I am stuck here and I did not get any specific solved post on stackoverflow.
Thanks in advance
Pointer on method requires instance when called, so your code might look like:
(bcond[i].*(bcond[i].func2bond[j]))(f, 1, 2);
or, splitting expression:
auto methodPtr = bcond[i].func2bond[j]; // auto is `void (BoundaryConditions::*) (Face &, int, int)`
(bcond[i].*methodPtr)(f, 1, 2);

Getting a error when creating a map of callbacks inside a class

The code:
class Base{
enum eventTypes{ EVENT_SHOW };
std::map<int, boost::function<bool(int,int)> > m_validate;
virtual void buildCallbacks();
bool shouldShowEvent(int x, int y);
};
void Base::buildCallbacks(){
m_validate[ EVENT_SHOW ] = boost::bind(&Base::shouldShowEvent,this);
}
I get the following error:
In base.cxx
return (p->*f_);
Error: a pointer to a bound function may only be used to call
the function (boundfuncalled)
I get what the error is saying, I am not allowed to do anything else but call the bounded member function, but how can I circumvent that issue? I'm not sure why this shouldn't work.
m_validate[ EVENT_SHOW ] = boost::bind(&Base::shouldShowEvent,this);
The call to bind() produces a function object that takes no arguments. You can't use such an object to call Base::shouldShowEvent, because it takes two arguments. So you have to turn the function object into one that takes two arguments:
m_validate[ EVENT_SHOW ] = boost::bind(&Base::shouldShowEvent,this, _1, _2);
(Not tested...)

C++ - Smart Pointers - Passing derived class shared pointer to base through template

I have the following and having difficulty resolving the error please help.
i have the following class as template definition somewhere.
template<class ConcreteHandlerType>
class SomeAcceptor: public ACE_Acceptor<ConcreteHandlerType, ACE_SOCK_Acceptor>
In some other file, i initialize this class in the constructor
class initialize {
typedef SomeAcceptor<BaseClassSomeHandler> baseAcceptor_t;
typedef SomeAcceptor<DerivedClassSomeHandler> derivedAcceptor_t;
boost::shared_ptr<baseAcceptor_t;> mAcceptor;
boost::shared_ptr<derivedAcceptor_t;> mDerivedAcceptor;
bool HandleAcceptNotification(BaseClassSomeHandler& someHandler);
initialize() : mAcceptor(0), mDerivedAcceptor(new DerivedAcceptor_t) {
mAcceptor->SetAcceptNotificationDelegate(fastdelegate::MakeDelegate(this, &initialize::HandleAcceptNotification));
}
}
Error i get is
error: no matching function for call to `boost::shared_ptr<SomeAcceptor<BaseClassSomeHandler> >::shared_ptr(int)'common/lib/boost_1_39_0/boost/smart_ptr/shared_ptr.hpp:160: note: candidates are: boost::shared_ptr<SomeAcceptor<BaseClassSomeHandler> >::shared_ptr(const boost::shared_ptr<SomeAcceptor<BaseClassSomeHandler> >&)
common/lib/boost_1_39_0/boost/smart_ptr/shared_ptr.hpp:173: notboost::shared_ptr<T>::shared_ptr() [with T = SomeAcceptor<BaseClassSomeHandler>]
I also tried overloading the function with
bool HandleAcceptNotification(DerivedClassSomeHandler& someHandler);
but because mAcceptor is of type SomeAcceptor BaseClassSomeHandler, i get this error, but to fix this.
I guess i need to cast it somehow, but how to do it?
i tried doing like below inside the constructor and it didn't work
initialize() : mAcceptor(0), mDerivedAcceptor(new DerivedAcceptor_t) {
mAcceptor = mDerivedAcceptor; // Error here
mAcceptor->SetAcceptNotificationDelegate(fastdelegate::MakeDelegate(this, &initialize::HandleAcceptNotification));
}
From your code, it looks like you want mAcceptor to be assigned NULL (0), if that is the case you don't need to initialize it at all, as the default constructor will take care of that. But, since you call a function on that (NULL) pointer immediately, its not immediately clear exactly what you want to do.
If you want mAcceptor and mDerivedAcceptor to point to the same (shared) object and assuming DerivedClassSomeHandler is derived from BaseClassSomeHandler, this is a situation where you should use boost::shared_static_cast, as described here.
There's also some good information in this apparently related question.
The error is due to the mAcceptor(0) in
initialize() : mAcceptor(0), mDerivedAcceptor(new DerivedAcceptor_t) {
mAcceptor->SetAcceptNotificationDelegate(fastdelegate::MakeDelegate(this, &initialize::HandleAcceptNotification));
}
The smart_ptr default constructor assigns the wrapped ptr to NULL, so leave out mAcceptor(0) from the initialization list.
boost::shared_ptr<SomeAcceptor<BaseClassSomeHandler> >::shared_ptr(int)
It's yelling at you that there's no constructor that accepts an int.
Just use: mAcceptor()

error: no matching function for call to

i want to use a method of anothre class in another one,but i get error below,whats the problem?
TIA
error: no matching function for call to ‘PositionInfo::PositionInfo()’
here is my code:
PositionInfo Pos;
double metr=Pos.GetBallDistToTeammate(5);
and PositionInfo.h class is:
PositionInfo(WorldState *pWorldState, InfoState *pInfoState);
and PositionInfo.cpp class is:
const double & GetBallDistToTeammate(Unum unum) const { Assert(unum > 0); return GetBallDistToPlayer(unum); }
Default constructor PositionInfo::PositionInfo() { /* code */} is missing in your cpp file.
error: no matching function for call to ‘PositionInfo::PositionInfo()’
This seems like someone tries to call a default constructor for the class, but the compiler cannot find one.