function-style conversion to builtin type takes only one argument
2016-10-20 14:15
387 查看
今天同事遇到一个问题:error C2564:‘xxxx’:a function-style conversion to a built-in type can only take one argument
之前没有遇到过,于是到网上查了一下,发现如下文章的error场景与他的十分类似:
http://forums.codeguru.com/showthread.php?327586-C-Newbie-What-have-I-done-wrong
文章中提到的原因是:
typedef 定义的函数指针,是一种类型。我们调用方法要使用该类型的变量。
下面是我的测试:
dll:
头文件:其中DLLEXPORT是定义在stdafx.h中 #define DLLEXPORT
源文件
调用dll的控制台工程代码:
编译结果:
error C2564: 'mySend' : a function-style conversion to a built-in type can only take one argument
如测试结果所示:使用函数指针的定义直接当作函数来调用,确实是不行的。
其实导致错误的原因很简单,就是没有正确使用函数指针。
另外,上述测试代码中使用dll的方式,也值得我们参考:
1、它是动态加载dll的,
2、它没有依赖dll的头文件,仅仅是通过GetProcAddress指定的函数名定位到函数的。
3、typedef定义的函数指针,让我们知道dll的本质,我只要知道你的接口(dll)是什么样子的,我就可以调用你。
调用dll的过程非常简洁,我们可以应用到自己的项目中。
之前没有遇到过,于是到网上查了一下,发现如下文章的error场景与他的十分类似:
http://forums.codeguru.com/showthread.php?327586-C-Newbie-What-have-I-done-wrong
文章中提到的原因是:
typedef 定义的函数指针,是一种类型。我们调用方法要使用该类型的变量。
下面是我的测试:
dll:
头文件:其中DLLEXPORT是定义在stdafx.h中 #define DLLEXPORT
#ifdef DLLEXPORT #define MY_API _declspec(dllexport) #else #define MY_API _declspec(dllimport) #endif extern "C" { MY_API int send(char* thisNumber, int flag); }
源文件
#include "stdafx.h" #include "ErrorTestDll.h" #include <iostream> int send(char* thisNumber, int flag) { printf("hello world! number:%c, flag:%d \n", *thisNumber, flag); return 1; }
调用dll的控制台工程代码:
#include "stdafx.h" #include <Windows.h> typedef int (*mySend)(char* thisNumber, int flag); int _tmain(int argc, _TCHAR* argv[]) { HINSTANCE dllHandle = NULL; dllHandle = LoadLibrary("E:\\test\\ErrorTestDll\\Debug\\ErrorTestDll.dll"); mySend fp = (mySend)GetProcAddress(dllHandle, "send"); mySend fp2; char ch = 'A'; //int t = fp(&ch, 1); // 正确的调用形式 //int t = fp2(&ch, 1); int t = mySend(&ch, 1); // 文中出错是由于这样的调用。 printf("t : %d", t); while(1); return 0; }
编译结果:
error C2564: 'mySend' : a function-style conversion to a built-in type can only take one argument
如测试结果所示:使用函数指针的定义直接当作函数来调用,确实是不行的。
其实导致错误的原因很简单,就是没有正确使用函数指针。
另外,上述测试代码中使用dll的方式,也值得我们参考:
1、它是动态加载dll的,
2、它没有依赖dll的头文件,仅仅是通过GetProcAddress指定的函数名定位到函数的。
3、typedef定义的函数指针,让我们知道dll的本质,我只要知道你的接口(dll)是什么样子的,我就可以调用你。
调用dll的过程非常简洁,我们可以应用到自己的项目中。
相关文章推荐
- Only one complex type allowed as argument to a web api controller action.
- TypeError in Threading. function takes 1 positional argument but 100 were given
- Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.
- g++编译warning: deprecated conversion from string constant to ‘char*’ /tmp/ccSf0DmE.o: In function `ma
- ReactJs 报错 Element type is invalid: expected a string (from built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `Me`.
- The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
- There is only one success --- to be able to spend your life in your own way.
- Function to Convert Only First Letter Capital of each word in a sentence
- conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
- Element type is invalid: expected a string (for built-in components) or a class/function (for compos
- python3 translate() takes exactly one argument (2 given) in python error
- Maven用Scala编译,bad symbolic reference. A signature in package.class refers to type compileTimeOnly
- [WTL]use thunk in ATL to invoke C-style CALLBACK function
- The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. 错误的原因及解决方案
- waring:in_array()[function.in-array]:wrong datatype for second argument
- [Tip: double string]Conversion from string to built-in types like double
- Mutex : Using Mutex to only lunch one instance in one machine
- How to test only one benchmark function?
- Reporting Services - Export Data to Excel In Only One Sheet
- How to fix the python problem 'xxx() takes exactly one argument'