gpt4 book ai didi

c++ - va_list 无法获取任何类型

转载 作者:行者123 更新时间:2023-11-28 00:04:49 25 4
gpt4 key购买 nike

我有这样的代码

 #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 ora char*. ...

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

最佳答案

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

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

关于c++ - va_list 无法获取任何类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36369216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com