重载类似的转换
给定这两个函数:
bool logMessage(LogType_E, unsigned int, int, const char *, ...); //!< Log message with parameters
bool logMessage(LogType_E, int, const char *, ...); //!< Logs message with domain of Log class
并调用其中之一:
A3D_LOG_INSTANCE.logMessage(Log::LOG_INFO, 0, "Number = %d", 10);
错误 1 错误 C2666: 'AX::Base::Log::logMessage' : 2 个重载 相似的 转换 o:\AX_FusRecAlg\src\base\test.u\LogSimpleFileTest\LogSimpleFileTest.cpp 50 AX.Base.LogSimpleFileTest
有人可以用简单的英语向我解释为什么会出现此错误并可能提供替代方案吗?我不明白 char* 之前有 3 个参数的函数如何与有两个参数的函数匹配?!
谢谢。
编辑:
由于你们中的一些人想知道我隐藏了信息: 函数签名无法更改。无法使用任何模板。 只需解释为什么会出现此错误就足够了。
enum LogType_E {
LOG_ERROR = 0, //!< error
LOG_WARNING = 1, //!< warning
LOG_SUCCESS = 2, //!< success
LOG_INFO = 3, //!< info
LOG_TRACE = 4, //!< trace message if tracing is enabled
LOG_TRACE1 = 5, //!< trace level 1
LOG_TRACE2 = 6, //!< trace level 2
LOG_TRACE3 = 7, //!< trace level 3
LOG_TRACE4 = 8 //!< trace level 4
};
bool logMessage(LogType_E, unsigned int, int, const char *, ...)
{
return true;
}
bool logMessage(LogType_E, int, const char *, ...)
{
return true;
}
int main()
{
logMessage(LOG_TRACE, 0, 0, "Teststring 2");
return 0;
}
将以上代码复制并粘贴到 .cpp 文件中并运行它或单击此处。
Given these two functions :
bool logMessage(LogType_E, unsigned int, int, const char *, ...); //!< Log message with parameters
bool logMessage(LogType_E, int, const char *, ...); //!< Logs message with domain of Log class
And calling one of them :
A3D_LOG_INSTANCE.logMessage(Log::LOG_INFO, 0, "Number = %d", 10);
Error 1 error C2666: 'AX::Base::Log::logMessage' : 2 overloads have
similar
conversions o:\AX_FusRecAlg\src\base\test.u\LogSimpleFileTest\LogSimpleFileTest.cpp 50 AX.Base.LogSimpleFileTest
Could someone explain to me in plain English why this error occurs and possibly offer an alternative? I don't understand how the function which has 3 arguments before the char* matches with the function which has two arguments?!
Thanks.
Edit :
Since some of you are wondering that I am hiding information :
The function signature cannot be changed. No templates can be used. Just an explanation of why this error occurs would suffice.
enum LogType_E {
LOG_ERROR = 0, //!< error
LOG_WARNING = 1, //!< warning
LOG_SUCCESS = 2, //!< success
LOG_INFO = 3, //!< info
LOG_TRACE = 4, //!< trace message if tracing is enabled
LOG_TRACE1 = 5, //!< trace level 1
LOG_TRACE2 = 6, //!< trace level 2
LOG_TRACE3 = 7, //!< trace level 3
LOG_TRACE4 = 8 //!< trace level 4
};
bool logMessage(LogType_E, unsigned int, int, const char *, ...)
{
return true;
}
bool logMessage(LogType_E, int, const char *, ...)
{
return true;
}
int main()
{
logMessage(LOG_TRACE, 0, 0, "Teststring 2");
return 0;
}
Copy and paste above code into a .cpp file and run it or click here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是正常行为。从您的示例来看:
第二个参数可以是
int
和unsigned int
。您必须进行显式强制转换才能使其正常工作。
例如:
This is normal behaviour. From your example:
Second parameter can be both,
int
andunsigned int
.You will have to make explicit cast to make it work.
For example: