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]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2020-01-01 C# compare different Encoding pattern between UTF8 and UTF32 based on Md5