ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function
//model/util.h #pragma once #ifndef __util_h__ #define __util_h__ #include <chrono> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <sstream> #include <string.h> #include <thread> #include <unistd.h> #include <uuid/uuid.h> #include <vector> using namespace std; class util { public: string get_time(); static int sum(int x,int y); static int prod(int x,int y); int pass_func_as_argu(int x,int y,int(*func)(int,int)); void invoke_func_argv(int x,int y); }; #endif //model/util.cpp #include "model/util.h" void util::invoke_func_argv(int x,int y) { cout<<"The sum of "<<x<<" and "<<y<<" is "<<pass_func_as_argu(x,y,&sum)<<endl; cout<<"The prod of "<<x<<" and "<<y<<" is "<<pass_func_as_argu(x,y,&prod)<<endl; cout<<get_time()<<",finished in "<<__FUNCTION__<<","<<__LINE__<<endl; } int util::sum(int x, int y) { return x + y; } int util::prod(int x, int y) { return x * y; } int util::pass_func_as_argu(int x, int y, int (*func)(int, int)) { return func(x, y); } string util::get_time() { chrono::time_point<chrono::high_resolution_clock> now = chrono::high_resolution_clock::now(); chrono::milliseconds mills = chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch()); chrono::seconds seconds = chrono::duration_cast<chrono::seconds>(now.time_since_epoch()); uint64_t millsValue = mills.count() - seconds.count() * 1000; time_t rawTime = chrono::high_resolution_clock::to_time_t(now); struct tm tmInfo = *std::localtime(&rawTime); stringstream ss; ss << std::put_time(&tmInfo, "%Y%m%d%H%M%S") << std::setfill('0') << std::setw(3) << millsValue; string dtvalue = ss.str(); ss = stringstream(std::string()); return dtvalue; } //main.cpp #include "model/util.h" void invoke_func_argv_demo(int x,int y) { util ul; ul.invoke_func_argv(x,y); } int main(int args,char **argv) { invoke_func_argv_demo(atoi(argv[1]),atoi(argv[2])); }
Compile
g++ -g -std=c++2a -I. *.cpp ./model/*.cpp -o h1 -luuid -lpthread -ljsoncpp
Run
If I remove the static modifier of sum method as below.
//model/util.h #pragma once #ifndef __util_h__ #define __util_h__ #include <chrono> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <sstream> #include <string.h> #include <thread> #include <unistd.h> #include <uuid/uuid.h> #include <vector> #include <jsoncpp/json/json.h> using namespace std; class util { public: int sum(int x,int y); static int prod(int x,int y); int pass_func_as_argu(int x,int y,int(*func)(int,int)); void invoke_func_argv(int x,int y); }; #endif
Then compile as below g++ command while it failed after I remove the static modifier of sum method declaration
And reported below error
error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&util::sum’ [-fpermissive]