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.
Related
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>();
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
I'm trying to implement more flexibility in my numerics by allowing me to choose different forms of a mathematical function and vary their parameters through instantiating them as objects of a certain class. That class includes certain mathematical functions I may choose plus parameters that I can vary. The constructor of the class sets a member function pointer in the class to a member function according to what mathematical function I want. I want to solely use the pointer to call whatever function it points to by directly using the pointer in my routine.
However, that proved daunting as I didn't know that member function pointers require a certain syntax and seem to work somewhat differently from regular function pointers according to what I could gather. I've experimented quite a bit and constructed myself a minimal example shared below.
#include<iostream>
#include<string.h>
#include<cstdlib>
#include<stdio.h>
class Someclass
{
public:
// constructor to set pointer
Someclass(std::string);
// member function pointer to hold functions
void (Someclass::*fptr)();
// auxiliary function to call testfunction via pointer
void call ();
// testfunction
void foo();
};
// testfunction
void Someclass::foo()
{
printf("foo says hi! \n");
}
// call via specific function
void Someclass::call()
{
(this->*fptr)();
}
// constructor
Someclass::Someclass(std::string name)
{
if(name=="foo")
{
this->fptr = &Someclass::foo;
}
}
int main()
{
Someclass someobject("foo");
someobject.foo(); // direct testfunction call: Works OK
someobject.call(); // call via auxiliary function: Works OK
//(someobject.*fptr)(); // direct pointer dereferencing: Gives Error
return(EXIT_SUCCESS);
}
It shows that I can access the pointer by use of another member function that just calls whatever the pointer points to via use of a this pointer. However, I still can't seem to get the function call to work if I try to use the pointer directly in my main function through the line,
(someobject.*fptr)()
This particular expression leads to my compiler complaining about the scope and if I include the class scope, the compiler mentions invalid use of non-static members. Still, I'm confused as to why my implementation here doesn't work and if it does, how the proper syntax in my problem would be and why that has to be so.
Any insights would be really appreciated.
fptr is a member of the object, not a variable local to main. In such respect member function pointers behave exactly the same as all other variable types. You were so close, and just need to qualify the function pointer name with the object name:
(someobject.*(someobject.fptr))();
The reasons for this is .* indicates a pointer to member function and does not directly reference the members of an object like the . and .-> operators. Since fptr is a member of Someclass and not a local variable you need to reference it directly like so
(someobject.*someobject.fptr)();
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
//mediafactory.h
class MediaFactory{
public:
typedef Media* (*funPointer)();
funPointer somePointer;
}
//mediafactory.cpp
Media* MediaFactory::returnMedia(){
}
when I try to do
somePointer = returnMedia;
I get this error:
1 IntelliSense: a value of type "Media *(MediaFactory::*)()" cannot be assigned to an entity of type "MediaFactory::funPointer" c:\Users\...\mediafactory.cpp 37
However, if i change the function to the code below, it will compile and work
Media* returnMedia(){ //without scope
}
MediaFactory::returnMedia() is a non-static member function, and can only be called via an instance of MediaFactory.
typedef Media* (*funPointer)(); defines a pointer to a static or non-member function, which is called without any object. It's therefore a different type, incompatible with a pointer to a member function.
If you don't need it to point to a member function, then make returnMedia() either static or a non-member (as you note in at the end of the question).
If you do need it to point to a member function, then you need a pointer-to-member:
typedef Media* (MediaFactory::*funPointer)();
which can be set to point to a member function:
somePointer = &MediaFactory::returnMedia;
and can be called on a factory object:
Media * media = (someFactory.*somePointer)();
Alternatively, you might want to bind the function pointer to an object when you set the pointer, rather than when you use it. In this case, you could use std::function (or boost::function if you're stuck with an outdated compiler) to store an arbitrary callable object:
typedef std::function<Media*()> function;
function someFunction;
someFunction = std::bind(&MediaFactory::returnMedia, &someFactory);
Media * media = someFunction();
You cannot assign a method of a class to a global function pointer without an instance of this class.