C++:关于 const char* 和 printf 的一些错误

C++: some errors about const char* and printf

本文关键字:printf 错误 关于 const char C++      更新时间:2023-10-16

我找到了从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 会更正常。