123iamking
123iamking

Reputation: 2703

va_list can't get any type

I have a code like this

 #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);
}

But what I got when I run the code is

This should be right 0.00 = 700.0

I read this article and I got

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

How can I fix this? The function str.Format can do it right so I know there must be a way - I have read the source code of the Format function but I still don't know how to fix it. Thank for reading :)

Upvotes: 0

Views: 113

Answers (1)

awesoon
awesoon

Reputation: 33651

You should use CString::FormatV instead - it accepts va_list as the second argument. Passing va_list to the CString::Format is a bad idea, because it creates another va_list with va_list inside.

It is common to implement two methods: one with variadic number of arguments and another with va_list.

Upvotes: 3

Related Questions