gpt4 book ai didi

c++ - 为什么 va_start 会失败?

转载 作者:可可西里 更新时间:2023-11-01 09:40:47 27 4
gpt4 key购买 nike

我想创建一些日志记录,我创建了一个类。但是我在将参数传递给它时遇到了一些问题。

类:

namespace debug
{
class log
{
private: // Members
const std::string context;
int Type;
public: // Methods
void message( int Type, const std::string& message, ... );

public: // Constructor, Destructor
log( const std::string& context, int Type );
~log();
};//class log
}//namespace debug

namespace debug
{
void log::message( int Type, const std::string& message, ... )
{
va_list args;
int len;

char *buffer;

va_start( args, message );

len = _vscprintf( message.c_str(), args ) + 1; // _vscprintf doesn't count terminating '\0'
buffer = ( char* )malloc( len * sizeof( char ) );

vsprintf_s( buffer, len, message.c_str(), args );

va_end( args );

}//log::message
}//namespace debug

我定义了两个宏:

#define DEBUG_METHOD( Type )  debug::log _debugLog( __FUNCTION__, Type );
#define DEBUG_MESSAGE( Type, debug_message, ... ) { _debugLog.message( Type, debug_message, ##__VA_ARGS__ ); }

我在这样的函数中使用它们:

BOOL test( BOOL *bTestVal)
{
DEBUG_METHOD( INFO );
DEBUG_MESSAGE( INFO, "Argument1 = '%s'", BoolToString( ( BOOL )*bTestVal) );

//here comes some work...
}

不幸的是,我总是得到一个错误。此行 len = _vscprintf( message.c_str(), args ) + 1; 总是会引发错误。 I think va_start is causing this, becasue args has a value of + args 0x0052eed8 "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ... char *

有人可以帮我解决我做错了什么吗?

提前致谢!

最佳答案

18.10/3 ...The parameter parmN is the identifier of the rightmost parameter in the variable parameter list of the function definition (the one just before the ...). If the parameter parmN is declared with a function, array, or reference type, or with a type that is not compatible with the type that results when passing an argument for which there is no parameter, the behavior is undefined.

强调我的。

关于c++ - 为什么 va_start 会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21740768/

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