This question already has answers here:
C++ Call Pointer To Member Function
(4 answers)
Function pointer to member function
(8 answers)
How to call through a member function pointer?
(2 answers)
Calling a function pointer on a member function [duplicate]
(1 answer)
Closed 17 days ago.
So I have a vector of a struct object containing a function pointer to an external class.
//Menu.h
#include "Byte.h"
struct menuItem
{
Byte (Byte::*funcPoint)(Byte&);
string descript;
};
class Menu
{
private:
vector<menuItem> menuVect;
void runSelection(Byte&, Byte&);
public:
Menu();
void addMenu(string Description, Byte (Byte::*f)(Byte&));
void runMenu(Byte&, Byte&);
void waitKey();
};
I can assign new instances of function pointers as seen here:
void Menu::addMenu(string Description, Byte(Byte::*f)(Byte&))
{
menuVect.push_back({f, Description});
}
But when it comes time execute any given function within the vector, I get an error
void Menu::runSelection(Byte& bX, Byte& bY)
{
int select;
cin >> select;
if (select > 0 && select < menuVect.size() + 1) {
cout << bX.toInt();
menuVect.at(select - 1).funcPoint();
}
else
exit(0);
waitKey();
}
Not sure if its necessary, but I'll include main as well, as one of the function pointers being assigned to the vector
Main
#include "Menu.h"
int main()
{
Menu m;
Byte b1(7);
Byte b2(3);
m.addMenu("1. Func 1", Byte::add);
m.addMenu("2. Func 2", Byte::sub);
m.addMenu("3. Func 3", Byte::mul);
m.addMenu("4. Func 4", Byte::div);
m.runMenu(b1, b2);
return 0;
}
Byte Byte::add(int val)
{
Byte b(this->toInt() + val);
return b;
}
I have already found an obvious work around, by simply not having the functions within an object
The particular errors I'm getting are:
Error (active) E0109 expression preceding parentheses of apparent call must have (pointer-to-) function type
(field)std::vector Menu::menuVect
Error C3867 'Byte::add': non-standard syntax; use '&' to create a pointer to member
But nothing I find when I look this up helps.
Related
This question already has answers here:
How to call a function by its name (std::string) in C++?
(4 answers)
Closed 1 year ago.
I am very kindergartener to C++. Hope someone can help me out with my problem.
Assume there is a function defined in one class.
void __foo__(int x, int y){
//do something
}
In another class, there is a char pointer that holds the value of the function name.
static const char *func = "__foo__";
How do I call the function by using the "func" like func(0, 0)?
C++ doesn't have reflection, so you have to write the code to call the function based on the name yourself
void fall_function_based_on_name(const char* func_name, classWithMethod* self, lint x, int y) {
if (strcmp(func_name, "__foo__")==0)
self->__foo__(x, y);
else
throw std::logic_error("method name not found");
}
This question already has answers here:
How to call through a member function pointer?
(2 answers)
Closed 5 years ago.
I'm trying to call a pointer to a member function, which I retrieve from a map. The call th.fpObjHandler(md, evd, tokenIdx) seems erroneous, I've tried various different syntaxes (e.g. .* and ->*), but I cannot seem to get it right. Hopefully somebody here is able to help.
struct TokenHandler
{
int tokenIdx;
eventDataStatus_t (Schema::*fpObjHandler)(MessageData &md, EventDataImpl &evd, int &tokenIdx);
};
Schema::event(MessageData &md, EventDataImpl &evd)
{
int tokenIdx = 0;
std::map<std::string, TokenHandler> tokenHandlerMap;
tokenHandlerMap["timeInterval"] = { -1, &Schema::timeInterval };
// ..
// ....
TokenHandler th = tokenHandlerMap.at(key);
if (th.fpObjHandler != NULL) {
th.fpObjHandler(md, evd, tokenIdx); // THIS RESULTS IN ERROR
//..
//...
}
}
eventDataStatus_t Schema::timeInterval(MessageData &md, EventDataImpl &evd, int &tokenIdx)
{
//..
//...
return OK;
}
Schema.cpp:111:54: error: must use ‘.’ or ‘->’ to call
pointer-to-member function in ‘th.TokenHandler::fpObjHandler (...)’,
e.g. ‘(... ->* th.TokenHandler::fpObjHandler) (...)’
th.fpObjHandler(md, evd, tokenIdx);
^
First you need an instance of the class Schema, then use .* or ->*, something like:
Schema schema;
(schema.*th.fpObjHandler)(md, evd, tokenIdx);
Or, as you are already in a method of Schema:
(this->*th.fpObjHandler)(md, evd, tokenIdx);
This question already has answers here:
Do class functions/variables have to be declared before being used?
(5 answers)
Closed 6 years ago.
is it possible to use a member variable of a class before declaring it?
here is my code.
using namespace std;
class Computer
{
public:
Computer()
{
processor_speed = 0;
}
~Computer();
void setspeed (int);
int getspeed (void);
private:
int processor_speed;
};
/*Computer::Computer()
{
processor_speed = 0;
} */
Computer::~Computer()
{
cout<<"this is a destructor"<<endl;
}
void Computer:: setspeed(int p)
{
processor_speed = p;
}
int Computer::getspeed(void)
{
return processor_speed;
}
int main(void)
{
Computer obj;
cout<<"processor speed is "<<obj.getspeed()<<endl;
obj.setspeed(100);
cout<<"processor speed is now "<<obj.getspeed()<<endl;
return 0;
}
as u can see here i was able to use variable processor_speed before declaring it.
i saw a similar question here: Do class functions/variables have to be declared before being used?
but i was not able to understand the reason why this code work.
Thanks
Yes, you can do it.
A member variable is in scope for member functions of your class even if it is textually after its first point of use. The compiler translates your code in several "passes". One could think of it as getting all member variables first, and only then translating member functions, with all declarations in place.
Note that this is not allowed for "free-standing" global and static variables inside translation units: a declaration must precede the first use, otherwise you get an error.
This question already has answers here:
How do I call a pointer-to-member-function?
(4 answers)
Closed 9 years ago.
I want to call member-functions through member-function-pointers. The calling function is also a member.
class A;
typedef int (A::*memFun)();
class A
{
int P(){return 1;}
int Q(){return 2;}
int R(){return 3;}
int Z(memFun f1, memFun f2)
{
return f1() + f2(); //HERE
}
public:
int run();
};
int A::run()
{
return Z(P, Q);
}
int main()
{
A a;
cout << a.run() << endl;
}
I am not doing it correctly, and am getting error-
main.cpp:15:19: error: must use '.*' or '->*' to call pointer-to-member function in 'f1 (...)', e.g. '(... ->* f1) (...)'
return f1() + f2(); //HERE
Please show the correct way to do it.
EDIT - there is another error, which is solved by having-
return Z(&A::P, &A::Q);
(this->*f1)() + (this->*f2)();
Regardless of whether you're calling it from inside the class, you have to explicitly specify the object on which to call (in this case this). Also note the required parentheses. The following is wrong:
this->*f1() + this->*f2()
Like this:
(this->*f1)() + (this->*f2)()
While you don't show it, you most likely have errors when calling Z as well.
You need to call the function like this:
Z(&A::P, &A::Q)
you have to use this
(this->*f1)();
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ member-function pointer
How to invoke pointer to member function when it's a class data member?
I've only recently started using C++, so I apologize if the following contains any trivial mistakes, or if I missed an easier solution. I would like to achieve something like this:
class ClassA {
typedef double (ClassA::*CondFunc)();
public:
ClassA(int x, int y) {
value_ = x;
switch (y) {
case 0:
condFunc_ = &ClassA::condA;
break;
case 1:
condFunc_ = &ClassA::condB;
default:
break;
}
}
~ClassA();
int value_;
CondFunc condFunc_;
double condA() { return 2.0*value_; }
double condB() { return 4.0*value_; }
void Test() {
int a = condFunc_(); // compile error
}
};
but get a compile error in Test(). Please note that this is a vastly simplified function and is not supposed to make any sense. I've searched this forum and elsewhere for answers, but am still not sure whether defining/calling such non-static member function pointers is even possible. The only plausible hint/solution I've come across employs a static wrapper function to achieve something similar. I'd be grateful for any help/clarifications.
You have to call the member pointer function like this:
int a = (this->*condFunc_)();