va_list 无法获取任何类型

va_list can't get any type

我有这样的代码

 #include "atlstr.h"

void DisplayMessage(CString pszFormat, ...)
{
    CString str;
    va_list argList;
    va_start(argList, pszFormat);
    str.Format(pszFormat, argList);
    va_end(argList);

    _tprintf(_T("%s"), str);
}

void main()
{           
    DisplayMessage("This should be right %.2f = 700.0", 700.0);

    //Stop to watch
    int i = 0;
    scanf_s("%d",i);
}

但是当我 运行 代码时我得到的是

This should be right 0.00 = 700.0

我读了这个 article 我得到了

... Notice that testit expects its second parameter to be either an int or a char*. ...

我该如何解决这个问题?函数 str.Format 可以做到这一点,所以我知道一定有办法 - 我已经阅读了 Format 函数的源代码,但我仍然不知道如何修复它。 感谢阅读:)

您应该改用 CString::FormatV - 它接受 va_list 作为第二个参数。将 va_list 传递给 CString::Format 是个坏主意,因为它会创建另一个 va_list,其中包含 va_list

实现两种方法很常见:一种具有可变数量的参数,另一种具有 va_list