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

ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function

 

 

 

Run

ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function

 

 

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

ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function

 

 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]

原文链接: https://www.cnblogs.com/Fred1987/p/17017631.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401173

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:09
下一篇 2023年4月19日 上午9:09

相关推荐