Cannot initialize a pointer using std::unique_ptr - c++

I have an object inside my class and I have declared the object without any initialization:
std::unique_ptr<tf::TransformBroadcaster> tfb_;
Then, during the construction, I have decided to initialize my tfb_:
tfb_ = std::make_unique<tf::TransformBroadcaster>(new tf::TransformBroadcaster());
I am getting an error:
error: no matching function for call to ‘tf::TransformBroadcaster::TransformBroadcaster(tf::TransformBroadcaster*)’
{ return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
From my understanding, it looks like I am trying to pass an argument even though I am not (or may be?). The header file of tf::TransformBroadcaster is nothing special (just a snippet):
class TransformBroadcaster{
public:
/** \brief Constructor (needs a ros::Node reference) */
TransformBroadcaster();
I have tried to use a raw pointer in my header file:
tf::TransformBroadcaster* tfb_;
and in my constructor:
tfb_ = new TransformBroadcaster()
and it worked. Any idea why?

As there is no constructor of TransformBroadcaster that takes a TransformBroadcaster* as input, you cannot call std::make_unique<TransformBroadcaster>() with such an argument.
In short, this line:
tfb_ = std::make_unique<tf::TransformBroadcaster>(new tf::TransformBroadcaster());
should be this:
tfb_ = std::make_unique<tf::TransformBroadcaster>();

Related

Initialise unique_ptr inside class

I want to initialise the unique pointer inside class after declaration and I tried few ways but unable to resolve the errors..
template <typename T>
struct Destroy
{
void operator()(T *t) const
{
t->destroy();
}
};
class Test
{
std::unique_ptr<IRuntime, Destroy<IRuntime>> runtime;
public:
Test()
{
/*
the function createIRuntime() return type is *IRuntime.
I tried using following but all the ways I got error:
1. runtime = std::make_unique<IRuntime, Destroy<IRuntime>> (createIRuntime());
2. runtime = createIRuntime();
3. runtime = std::unique_ptr<IRuntime, Destroy<IRuntime>> (createIRuntime());
Works fine if I do follow.
std::unique_ptr<IRuntime, Destroy<IRuntime>> runtime(createIRuntime());
*/
/* how to initialize the unique pointer here*/
}
};
runtime = std::make_unique<IRuntime, Destroy<IRuntime>> (createIRuntime());
Presumably IRuntime is an abstract class, which can't be constructed directly.
But even if it could be constructed as-is, only the 1st template parameter specifies the type to create. The 2nd and subsequent template parameters specify the types of parameters for the constructor that is called.
So, this statement is trying to call an IRuntime constructor that takes a Destroy<IRuntime> object as a parameter, passing a raw IRuntime* pointer to that parameter. No such constructor exists, so this fails to compile.
runtime = createIRuntime();
std::unique_ptr does not have an operator= that takes a raw pointer, only a std::unique_ptr. std::unique_ptr has a constructor that takes a raw pointer, but that constructor is marked explicit. So this fails to compile, too.
runtime = std::unique_ptr<IRuntime, Destroy<IRuntime>> (createIRuntime());
This is correct, and works just fine:
Online Demo
Another statement that works is:
runtime.reset(createIRuntime());
Online Demo
Also, since the code you showed is inside of another constructor, you can (and should) use that constructor's member initialization list:
Test() : runtime(createIRuntime())
{
}
Online Demo

MFC C++ Class member variable, use instead the constructor with parameter

This is MFC C++ (not C++ 0x11). I have a class, with a member variable of a CDerivedComboBox, call it, CMyForm. I want to make a constructor for CDerivedComboBox, that passes in a parameter, so when I declare it, I can either pass a flag or not.
For example in .h file, two constructors
CDerivedComboBox();
CDerivedComboBox(bool specialFlag);
It compiles fine when adding that constructor implementation, but in my class using it, I can't figure out how to actually create one calling that constructor. Is this not possible in C++/
CDerivedComboBox combo; // works
CDerivedComboBox comboParam(true); // does not work
The CDerivedComboBox should be a member of your CMyForm. It appears you want to dynamically call a different ctor for CDerivedComboBox at runtime. Therefore, I would recommend making the member be a pointer to a CDerivedComboBox:
class CMyForm
{
CDerivedComboBox *m_pMyCombo;
}
CMyForm::CMyForm(..., CWnd *pParent = NULL) : m_pMyCombo(NULL)
{
if (somecondition)
m_pMyCombo = new CDerivedComboBox();
else // use specialFlag
m_pMyCombo = new CDerivedComboBox(specialFlag);
}
Remember to delete m_pMyCombo in dtor!

C++ pointer to member function not a function pointer

Let's say I have the following definitions:
class ScriptInterpreter {
public:
class cell;
typedef ScriptInterpreter::cell (ScriptInterpreter::*proc_t) (const std::vector<cell> &);
class cell {
public:
proc_t proc;
};
ScriptInterpreter::cell proc_add(const std::vector<cell> & c);
};
And the following code going on:
ScriptInterpreter::eval(ScriptInterpreter::cell cell, environment * env)
{
// ...
ScriptInterpreter::cell c;
c.proc = &ScriptInterpreter::proc_add;
return (c.*proc_)(exps);
}
At the line where I try to call the function pointer I get the error
error: called object type 'proc_t' (aka 'ScriptInterpreter::cell (ScriptInterpreter::*)(const std::vector<cell> &)') is not
a function or function pointer
When I add the * in front of the func so the line looks like this:
ScriptInterpreter::cell c = (proc_cell.*proc_)(exps);
it produces this:
error: use of undeclared identifier 'proc_'
I already looked at Callback functions in c++ and other problems of that kind, but nothing really gave me a hint what's wrong or provided any information about my error. I definitely don't have any names twice or something of that kind.
Also after reading what is an undeclared identifier error and how do i fix it I'm pretty sure I got everything alright.
So what am I doing wrong?
Edit: updated the code with real code instead of placeholder code
In order to call a member function through a pointer of pointer-to-member type, you have to use either operator .* or operator ->*. And on the left-hand side you have to specify the object for which you want to invoke that member function.
In your case an attempt to do that could look as follows
A::B b_object;
b_object.func = &A::func_to_call;
A a_object;
A::B other_b_object = (a_object.*b_object.func)();
Note that since the pointer is declared as pointing to a member of A, the .* operator need an object of type A on the left-hand side.
However, in your specific case this is ill-formed since b_object.func is private and no accessible from main.
P.S. int main, not void main.

How to insert member function in vector? Getting Compilation error [duplicate]

I'm using C++ (not C++11). I need to make a pointer to a function inside a class. I try to do following:
void MyClass::buttonClickedEvent( int buttonId ) {
// I need to have an access to all members of MyClass's class
}
void MyClass::setEvent() {
void ( *func ) ( int );
func = buttonClickedEvent; // <-- Reference to non static member function must be called
}
setEvent();
But there's an error: "Reference to non static member function must be called". What should I do to make a pointer to a member of MyClass?
The problem is that buttonClickedEvent is a member function and you need a pointer to member in order to invoke it.
Try this:
void (MyClass::*func)(int);
func = &MyClass::buttonClickedEvent;
And then when you invoke it, you need an object of type MyClass to do so, for example this:
(this->*func)(<argument>);
http://www.codeguru.com/cpp/cpp/article.php/c17401/C-Tutorial-PointertoMember-Function.htm
You may want to have a look at https://isocpp.org/wiki/faq/pointers-to-members#fnptr-vs-memfnptr-types, especially [33.1] Is the type of "pointer-to-member-function" different from "pointer-to-function"?
you only need to add parentheses after the function call and pass arguments if needed

compilation error C++ : can not call member function without object

I have the following main file where I tried to create a map with predefined value and pass it for further processing by other method. The main file is as is shown below :
int main(){
map<id,Porto> _portoInit;
id = 1;
Porto p;
p.val = 5;
_portoInit.insert(pair<id, Porto>(id, p));
Porto::setPorto(_portoInit);
return 1;
}
where the setPorto is defined under a class as the following (in seperate file)
void Porto::setPorto( const map<id,Porto>& _portoblock ) {
//do stuffs
};
I got prompted with an error of "error: cannot call member function ... without object"
Did not I declare the object of _portoInit in the main file already or it is a wrong way of declaration?
You need to invoke the method through the actual object:
p.setPorto(_portoInit);
Unless setPorto is a static method, your code is invalid.
You should write
p.setPorto(_portoInit);
The "::" defines the scope of the function and is implicit in the above, as the object who's function is being called is a Porto.
setPorto is a non-static member function, so you need to call it on a Porto instance. For example:
p.setPorto(_portoInit);
Note that non-static member functions take an implicit first parameter of (possibly cv qualified) type T*, so you could have called it like this:
Porto::setPorto(&p, _portoInit);
In both cases, you need an object to call the member function on. This is what the compiler is telling you.