[c++] usage of the lib functional via /r/learnprogramming


[c++] usage of the lib functional

Hello, I experimented with <functional> and came across this error:

#include <iostream> #include <functional> void myTask1() { std::cout << "Hello: this is myTask1" << std::endl;} class something { public: void foo(){ std::cout << "Hello, this is foo" << std::endl;}; std::function<void()> TASK; }; int main() { something* a = new something(); a->TASK = myTask1; a->TASK(); // prints "Hello: this is myTask1" a->TASK = a->foo; // compiler Error <unresolved overloaded function type> } 

I'm not exactly sure, what to do here. The point of this is, that the base class has a pointer to a void function, which is supposed to be implemented in a derivated class.

Speaking of which, I also get a "not declared"-error, when I try doing something like this:

 class something { public: something() { TASK = foo; } private: void foo(){ std::cout << "Hello, this is foo" << std::endl;}; std::function<void()> TASK; }; 

Is there a way to forward-declare TASK and foo() while keeping them private?

Submitted July 17, 2017 at 02:19PM by Shrubberer
via reddit http://ift.tt/2uBwTdZ

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s