C++模板问题

发布于 2024-08-20 13:48:54 字数 1889 浏览 1 评论 0原文

我是 C++ 模板的新手。 我正在尝试一些小程序。

    CPP [80]> cat 000001.cpp 000001.hpp
#include <iostream>
#include <string>
#include "000001.hpp"

int main()
{
    int i = 42;
    std::cout << "max(7,i):   " << ::max(7,i) << std::endl;

    double f1 = 3.4;
    double f2 = -6.7;
    std::cout << "max(f1,f2): " << ::max(f1,f2) << std::endl;

    std::string s1 = "mathematics";
    std::string s2 = "math";
    std::cout << "max(s1,s2): " << ::max(s1,s2) << std::endl;
}

template <typename T>
inline T const& max (T const& a, T const& b)
{
        return  a < b ? b : a;
}

当我编译这个程序时:

我收到以下错误:

    CPP [78]> /opt/aCC/bin/aCC -AA 000001.cpp
Error (future) 229: "/opt/aCC/include_std/string.cc", line 164 # "Ambiguous overloaded function call; a
    function match was not found that was strictly best for ALL arguments. Two functions that matched
    best for some arguments (but not all) were "const unsigned long &max<unsigned long>(const unsigned
    long &,const unsigned long &)" ["000001.hpp", line 2] and "const unsigned long &std::max<unsigned
    long>(const unsigned long &,const unsigned long &)" ["/opt/aCC/include_std/algorithm", line 1762]."
    Choosing "const unsigned long &max<unsigned long>(const unsigned long &,const unsigned long &)"
    ["000001.hpp", line 2] for resolving ambiguity.
            _C_data = _C_getRep (max (_RW::__rw_new_capacity (0, this),
                                 ^^^
Warning:        1 future errors were detected and ignored. Add a '+p' option to detect and fix them before they become fatal errors in a future release. Behavior of this ill-formed program is not guaranteed to match that of a well-formed program

有人可以告诉我错误到底是什么吗?

I am new to templates in c++.
i was trying some small programs.

    CPP [80]> cat 000001.cpp 000001.hpp
#include <iostream>
#include <string>
#include "000001.hpp"

int main()
{
    int i = 42;
    std::cout << "max(7,i):   " << ::max(7,i) << std::endl;

    double f1 = 3.4;
    double f2 = -6.7;
    std::cout << "max(f1,f2): " << ::max(f1,f2) << std::endl;

    std::string s1 = "mathematics";
    std::string s2 = "math";
    std::cout << "max(s1,s2): " << ::max(s1,s2) << std::endl;
}

template <typename T>
inline T const& max (T const& a, T const& b)
{
        return  a < b ? b : a;
}

when i compile this program:

i get an error below:

    CPP [78]> /opt/aCC/bin/aCC -AA 000001.cpp
Error (future) 229: "/opt/aCC/include_std/string.cc", line 164 # "Ambiguous overloaded function call; a
    function match was not found that was strictly best for ALL arguments. Two functions that matched
    best for some arguments (but not all) were "const unsigned long &max<unsigned long>(const unsigned
    long &,const unsigned long &)" ["000001.hpp", line 2] and "const unsigned long &std::max<unsigned
    long>(const unsigned long &,const unsigned long &)" ["/opt/aCC/include_std/algorithm", line 1762]."
    Choosing "const unsigned long &max<unsigned long>(const unsigned long &,const unsigned long &)"
    ["000001.hpp", line 2] for resolving ambiguity.
            _C_data = _C_getRep (max (_RW::__rw_new_capacity (0, this),
                                 ^^^
Warning:        1 future errors were detected and ignored. Add a '+p' option to detect and fix them before they become fatal errors in a future release. Behavior of this ill-formed program is not guaranteed to match that of a well-formed program

Could nybody please tell me what exactly the error is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

审判长 2024-08-27 13:48:54

您可能在某处包含 而不是 。前者已经不存在一段时间了,但出于兼容性原因,编译器仍然接受包含并将其替换为

#include <iostream>
using namespace std;

这会导致 std::max 被引入全局命名空间,从而导致含糊不清。将 替换为 或重命名 max 函数,问题就会消失。

编辑:你显然已经修复了包含,但我敢打赌你仍然在某处using namespace std;。你需要摆脱它。事实上,您永远不应该在全局范围内使用using namespace

编辑:您可能还在某处使用 std::max 。你也需要摆脱它。

You are probably including <iostream.h> instead of <iostream> somewhere. The former hasn't existed for some time now, but for compatibility reasons, you compiler still accepts the include and replaces it with

#include <iostream>
using namespace std;

This causes std::max to be brought to the global namespace, thus resulting in an ambiguity. Replace <iostream.h> with <iostream> or rename your max function and the problem should disappear.

Edit: You've apparently fixed the include, but I bet you still have using namespace std; somewhere. You need to get rid of that. In fact you should never use using namespace in the global scope.

Edit: You might also have using std::max somewhere. You need to get rid of it too.

挥剑断情 2024-08-27 13:48:54

您发布的代码编译得很好,“000001.hpp”内部肯定还有其他问题。您也可以发布该文件的内容吗?

编辑:如果您按照 avakar 的说明进行操作,但问题仍然存在,则一定是由于您的编译器存在问题。我能想到两种明显的解决方法:将您的 max 函数重命名为其他名称,或者将其放入命名空间中:

namespace Foo
{
    template <typename T>
    inline T const& max (T const& a, T const& b)
    {
        return  a < b ? b : a;
    }
}

int main()
{
    int i = 42;
    std::cout << "max(7,i):   " << Foo::max(7,i) << std::endl;

    double f1 = 3.4;
    double f2 = -6.7;
    std::cout << "max(f1,f2): " << Foo::max(f1,f2) << std::endl;

    std::string s1 = "mathematics";
    std::string s2 = "math";
    std::cout << "max(s1,s2): " << Foo::max(s1,s2) << std::endl;
}

The code you've posted compiles just fine, there must be something else that is wrong inside "000001.hpp". Can you post the contents of that file too?

Edit: If you do as avakar says but the problem persists, that must be due to some problem with your compiler. There are two obvious workarounds I can think of: rename your max function to something else, or put it in a namespace:

namespace Foo
{
    template <typename T>
    inline T const& max (T const& a, T const& b)
    {
        return  a < b ? b : a;
    }
}

int main()
{
    int i = 42;
    std::cout << "max(7,i):   " << Foo::max(7,i) << std::endl;

    double f1 = 3.4;
    double f2 = -6.7;
    std::cout << "max(f1,f2): " << Foo::max(f1,f2) << std::endl;

    std::string s1 = "mathematics";
    std::string s2 = "math";
    std::cout << "max(s1,s2): " << Foo::max(s1,s2) << std::endl;
}
心在旅行 2024-08-27 13:48:54

我不知道您使用的是哪个编译器,但第二个错误告诉您以下两个函数发生冲突:

::max
std::max

这似乎很奇怪,您可能在某处有一个 using namespace std; 或更糟的是,那个如第一个错误中所述,您的包含使用 iostream.h 。您能否提供有关您的编译器/工具链以及 .hpp 文件内容的更多信息?

I don't know which compiler you are using but the second error tells you that the two following functions are clashing :

::max
std::max

It seems really weird, you may have a using namespace std; somewhere or worse, that one of your include use iostream.h as noted in the first error. Could you give more information about your compiler/toolchain and the content of your .hpp file?

背叛残局 2024-08-27 13:48:54

它说

max<unsigned long>

找到了两个定义。一个定义位于 000001.hpp 中,另一个定义位于 /opt/aCC/include_std/algorithm 中。编译器暂时选择了000001.hpp中的那个,所以现在不存在错误。但它说这两个定义将来可能会导致错误。

It says that two definitions for

max<unsigned long>

were found. One definition is in 000001.hpp and the other is in /opt/aCC/include_std/algorithm. The compiler chose the one in 000001.hpp for now, so no error is present now. But it says that these two definitions may cause errors in the future.

玩世 2024-08-27 13:48:54

我不知道这是否会导致问题,但无论如何;您不应该将名称 max 用于全局(或局部)函数,因为它是 STL 的一部分。

I don't know if this causes the problem, but anyhow; you shouldnt use the name max for your global (or local) function as it is a part of STL.

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