简体   繁体   English

错误:无法将&#39;print&#39;从&#39;void(*)(int)&#39;转换为&#39;std :: function <void()> “

[英]error: could not convert ‘print’ from ‘void (*)(int)’ to ‘std::function<void()>’

Following on from a previous question , I was pointed to an answer . 在上一个问题之后 ,我被指出了一个答案 The answer however involved a lambda function and I am trying to pass a normal function through. 但是答案涉及一个lambda函数,我正在尝试通过一个正常的函数。

Using the below code as an example: 以下面的代码为例:

#include <iostream>
#include <functional>

void forloop(std::function<void()> func, int arg) {
    func(arg);
}


void print(int number) {
    std::cout << number << std::endl;
}


int main() {
    int n = 5;
    forloop(print, n);
}

The following error is returned in CLion. CLion中返回以下错误。

/home/dave/JetBrains/CLion/bin/cmake/bin/cmake --build /home/dave/CLionProjects/csv/cmake-build-debug --target csv -- -j 2
Scanning dependencies of target csv
[ 50%] Building CXX object CMakeFiles/csv.dir/main.cpp.o
/home/dave/CLionProjects/csv/main.cpp: In function ‘void forloop(std::function<void()>, int)’:
/home/dave/CLionProjects/csv/main.cpp:5:13: error: no match for call to ‘(std::function<void()>) (int&)’
     func(arg);
             ^
In file included from /home/dave/CLionProjects/csv/main.cpp:2:0:
/usr/include/c++/6/functional:2122:5: note: candidate: _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = void; _ArgTypes = {}]
     function<_Res(_ArgTypes...)>::
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/6/functional:2122:5: note:   candidate expects 0 arguments, 1 provided
/home/dave/CLionProjects/csv/main.cpp: In function ‘int main()’:
/home/dave/CLionProjects/csv/main.cpp:16:21: error: could not convert ‘print’ from ‘void (*)(int)’ to ‘std::function<void()>’
     forloop(print, n);
                     ^
CMakeFiles/csv.dir/build.make:62: recipe for target 'CMakeFiles/csv.dir/main.cpp.o' failed
make[3]: *** [CMakeFiles/csv.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/csv.dir/all' failed
make[2]: *** [CMakeFiles/csv.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/csv.dir/rule' failed
make[1]: *** [CMakeFiles/csv.dir/rule] Error 2
Makefile:118: recipe for target 'csv' failed
make: *** [csv] Error 2

Apologies if this questions seems silly - I am a C++ noob. 道歉,如果这个问题看起来很傻-我是C ++新手。

std::function<void()> means "function that returns void and takes no arguments". std::function<void()>意思是“返回void且不带参数的函数”。 You're trying to pass print instead, which is a "function that returns void and takes an int ". 您正尝试通过print ,这是“返回void并采用int函数”。 The types are mismatched. 类型不匹配。

Change forloop to: forloop更改为:

void forloop(std::function<void(int)> func, int arg) { /* ... */ }

Also, don't use std::function to pass lambdas unless you have a good reason to do so. 另外,除非有充分的理由,否则不要使用std::function传递lambda。 I suggest reading my article on the subject: "passing functions to functions" . 我建议阅读有关该主题的文章: “将函数传递给函数”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将void *转换为std :: function <void()> - Convert void* to std::function<void()> 转换 std::function<void ()> 作废 (*)()</void> - Convert std::function<void ()> to void (*)() 使用void函数作为回调,从“ std :: function转换 <void ()> ? - Using a void function as a callback, convert from "std::function<void ()>? 无效打印功能出错 - Error in the void print function 错误:无法从 '<brace-enclosed initializer list> ' 到 'std::tuple<void (xxx::*)(), xxx> '</void></brace-enclosed> - error: could not convert from '<brace-enclosed initializer list>' to 'std::tuple<void (xxx::*)(), xxx>' C ++错误:[对二进制表达式(&#39;std :: map的无效操作数 <int, std::function<void ()> ,std :: less <int> ...] - C++ error:[ invalid operands to binary expression ('std::map<int, std::function<void ()>, std::less<int>…] 无法从“ void”转换为“ int” - cannot convert from “void” to “int” 无法从void转换为int - Cannot convert from void to int 错误:从&#39;int(*)(void *)&#39;到&#39;void *(*)(void *)&#39;的转换无效 - error: invalid conversion from ‘int (*)(void*)’ to ‘void* (*)(void*)’ 可以使用std :: bind1st将void(*)(void *,int)转换为void(*)(int)吗? - Can std::bind1st be used to convert void (*)(void*,int) to void (*)(int)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM