C++:关于 const char* 和 printf 的一些错误
C++: some errors about const char* and printf
我找到了从USB外围设备读取数据的代码:
#include "stdafx.h"
#define IWEARDRV_EXPLICIT
#include <windows.h>
#include <iweardrv.h>
int _tmain(int argc, _TCHAR* argv[])
{
// Load functions dynamically (in case they don't have a VR920)
HINSTANCE iweardll = LoadLibraryA("iweardrv.dll");
if (!iweardll) {
printf("VR920 drivers are not installed, you probably don't have a VR920.");
return 2;
}
IWROpenTracker = (PIWROPENTRACKER) GetProcAddress(iweardll, "IWROpenTracker");
IWRCloseTracker = (PIWRCLOSETRACKER) GetProcAddress(iweardll, "IWRCloseTracker");
IWRZeroSet = (PIWRZEROSET) GetProcAddress(iweardll, "IWRZeroSet");
IWRGetTracking = (PIWRGETTRACKING) GetProcAddress(iweardll, "IWRGetTracking");
IWRGetVersion = (PIWRGETVERSION) GetProcAddress(iweardll, "IWRGetVersion");
// Try to connect to the VR920 tracker
if (IWROpenTracker()) {
printf("VR920 is not connected.");
return 1;
}
// Read 20 samples
for (int i=1; i<=20; i++) {
LONG y, p, r;
double yaw, pitch, roll;
if (!IWRGetTracking(&y,&p,&r)) {
yaw = y*(180.0/32768.0);
pitch = p*(180.0/32768.0);
roll = r*(180.0/32768.0);
printf("Yaw=%lf degrees, Pitch=%lf degrees, Roll=%lf degrees", yaw, pitch, roll);
} else {
printf("Unable to read tracking.");
}
Sleep(500);
}
// Tidy up
IWRCloseTracker();
FreeLibrary(iweardll);
return 0;
}
我为包含文件iweardrv.h
设置了额外的包含目录。它返回我这些错误:
IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR"
IntelliSense: identifier "printf" is undefined
如何避免错误?第一个错误是指LoadLibrary
参数"iweardrv.dll"
(与iweardrv.h
相关的动态库),第二个错误是指所有printf
调用线路。
编辑:我使用LoadLibraryA()
更正了第一个错误,因为它需要const char*
但我无法更正第二个错误。
第一个错误是因为您使用定义的 UNICODE 进行编译,并且 LoadLibrary 需要一个宽字符串。使用 L 前缀指定宽文本:
LoadLibrary(L"iweardrv.dll");
第二个错误是由于缺少 #include。你需要包含stdio.h来定义printf:
#include <stdio.h>
对于C++来说,使用 std::cout 而不是 printf 会更正常。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- SIGSEGV, 分段错误. 而 printf() 数组索引的值
- printf 和 strftime 的参数错误无效
- 找到printf的意外错误,该格式为0.1F,用于四舍五入数字
- printf保持在循环外时工作正常,但在将循环保持在for循环中时会导致一些错误
- C++:关于 const char* 和 printf 的一些错误
- C++类型包装器 printf 打印错误的浮点值
- 错误地将字符串传递给printf样式的日志函数时缺少错误
- 添加 printf 语句时出现运行时错误
- printf和多个字符串的错误行为
- C++:printf中间的分段错误,但仅适用于Ubuntu
- 当传递64位enum给Printf时的错误
- 如果使用错误的格式字符串调用 printf,会发生什么情况