重载类似的转换

发布于 2024-12-14 15:28:02 字数 1693 浏览 2 评论 0原文

给定这两个函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春花秋月 2024-12-21 15:28:02

这是正常行为。从您的示例来看:

logMessage(LOG_TRACE, 0, 0, "Teststring 2");

第二个参数可以是 intunsigned int
您必须进行显式强制转换才能使其正常工作。

例如:

logMessage(LOG_TRACE, (unsigned int)0, 0, "Teststring 2");

This is normal behaviour. From your example:

logMessage(LOG_TRACE, 0, 0, "Teststring 2");

Second parameter can be both, int and unsigned int.
You will have to make explicit cast to make it work.

For example:

logMessage(LOG_TRACE, (unsigned int)0, 0, "Teststring 2");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文