日志(PCTSTR 格式,..)和日志(PCTSTR 文本):错误 C2668 对重载函数的不明确调用
Log(PCTSTR format,...) and Log(PCTSTR text): error C2668 ambiguous call to overloaded function
我定义了以下内容:
void LogMessage(PCTSTR text);
void LogMessage(PCTSTR format, ...);
如果我只想用一个参数调用函数,则会收到以下错误消息:
Source.cpp(10): error C2668: 'Log' : ambiguous call to overloaded function
could be 'void Log(PCTSTR,...)' or 'void Log(PCTSTR)'
while trying to match the argument list '(const wchar_t [42])'
是否可以对第一个版本进行static_cast以明确使用?或者除了重命名第一个或第二个函数之外,是否可以解决此问题?
下面怎么样?我还没有在VC ++(这似乎是您选择的平台(上进行测试,但希望您使用的版本实现了足够的C++11才能正常工作。
#include <iostream>
#include <cstdio>
#include <cstdarg>
void LogMessageWorker(char const* format, ...)
{
// 1k should be enough for anyone... ;)
char buf[1024] = { 0 };
// The version of vsnprint called should always null terminate correctly and doesn't
// strictly need the -1 but I believe that the implementation that is included with
// VC++ leaves a lot to be desired so you may need to slightly tweak this.
va_list args;
va_start (args, format);
vsnprintf (buf, sizeof (buf) - 1, format, args);
va_end (args);
std::cout << "LogMessage: " << buf << std::endl;
}
template <class... Arguments>
void LogMessage(char const* format, Arguments... arguments)
{
LogMessageWorker (format, std::forward<Arguments>(arguments)...);
}
void LogMessage(char const* text)
{
LogMessageWorker ("%s", text);
}
int main(int argc, char **argv)
{
LogMessage ("The test is starting...");
for (int i = 0; i < 3; i++)
LogMessage ("This is test #%d", i);
LogMessage ("This contains the % character and still it works (%d-%d-%d-%d)");
return 0;
}
相关文章:
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 是否可以配置提升日志刷新?
- 跟踪日志中的T.11803()是什么意思?
- 加快在C++中读取/处理日志文件的速度
- 如何将消息时间戳写入日志文件?
- 分析包含 NMEA 句子的日志文件C++
- 如何修复输出日志中的"EnableInput can only be specified on a Pawn for its Controller"错误
- 如何实现具有多个平台__FILE__和__LINE__信息的 C/C++ 可变参数日志记录宏?
- Log4net,将日志消息从 c++ dll 发送到 c# 应用程序?
- 返回ERROR_INVALID_PARAMETER的事件日志函数
- 为什么系统日志有两个不同的函数声明?
- 从更改日志获取最新的 USN 数据
- 如何从日志文件中抓取状态代码?(在 C++ 中)
- 未创建日志文件
- C++ 中混合二进制/文本日志记录的最佳做法
- 将日志宏转换为目标 C 字符串
- 轻松日志记录++如何避免多个初始化
- 尝试使用 EvtSetChannelConfigProperty() 函数更新最大事件日志文件大小时插入的错误值
- 输出 c++ 窗口 wstring 日志在终端中正确
- 日志(PCTSTR 格式,..)和日志(PCTSTR 文本):错误 C2668 对重载函数的不明确调用