首页 > 编程语言 >ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to

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

时间:2023-01-01 00:12:01浏览次数:44  
标签:function util non func int member static __ include

//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]

标签:function,util,non,func,int,member,static,__,include
From: https://www.cnblogs.com/Fred1987/p/17017631.html

相关文章

  • parentComponent.ctx.deactivate is not a function
    vue3中使用keep-alive报错TypeError:parentComponent.ctx.deactivateisnotafunction代码:<router-view#default="{Component,route}"><keep-aliv......
  • The Working Principle and Function of the Relay
    Onmanyhouseholdappliances(suchasautomaticwashingmachines,electricfurnaceheating,etc.),inordertopreventtheelectricalappliancesfrombeingdam......
  • python中global 和 nonlocal 的作用域
    python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量。一globalglobal关键字用来在函数或其他局部作用域中使用全局变量。......
  • rayTracingInOneWeekend
    前言本系列总结自raytracing.github.io,需要一定的图形学基础,不懂的同学可以移至GAMES101-现代计算机图形学入门-闫令琪_哔哩哔哩_bilibili这个版本的光线追踪非常适合初......
  • Docker中的<none>镜像
    背景:docker使用多了后,会发现,docker中会存在很对<none>镜像,也不知道能不能删。 经过查阅资料:docker的none进行分为两种,一种是dockerimages直接能看到的,另一种是需要dock......
  • SqlCommand.ExecuteNonQuery 方法
    一、ExecuteNonQuery方法是什么?SqlCommand的一个类,用于包含update、insert、delete、select的Transact-sql语句中来修改数据库中的数据,并返回结果。 二、返回的结果是什......
  • Codeforces - 542C - Idempotent functions(思维 + 数学、*2000)
    542C-Idempotentfunctions(⇔源地址)目录542C-Idempotentfunctions(⇔源地址)tag题意思路AC代码错误次数:1tag*2000+构造+图论+数学。......
  • POJ 1080 Human Gene Functions
    POJ1080HumanGeneFunctions题意:给出两组\(DNA\)序列求它们的最大相似度每个字母与其他字母或自身和空格对应都有一个打分,求在这两个字符串中插入空格,让这两个字......
  • 获取当前函数名 __FUNCTION__ 的使用
    vs项目中见过这种获取当前函数名的调用。觉得挺方便的就记录一下。==============================================================转载地址:http://blog.csdn.net/daf......
  • Python Type Hint中Optional[str]=None和str=None的区别
    PythonTypeHint中Optional[str]=None和str=None的区别1问题来源在读到FluentPython,2edEdition,P260时产生了一些疑问:在Python中使用typehint时,以下两个声明有......