C++模板问题

问题描述:

我是新来的模板在c + +。 我正在尝试一些小程序。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 

能nybody请告诉我到底是什么错误?

你发布的代码编译得很好,在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; 
} 
+0

此变通办法起作用。 但我保持范围解析运算符的最大值,以便它不应该调用std :: max。即使那么为什么错误即将到来? – Vijay 2010-02-09 09:42:47

+0

原因是由avakar描述的。如果你的“000001.hpp”头只包含'max'的定义是真的,那么问题出在你的编译器上,当它不应该在全局名字空间中注入一些std符号时。 – Manuel 2010-02-09 09:53:14

你可能包括<iostream.h>代替<iostream>地方。前者已经不存在了一段时间了,但出于兼容性考虑,你编译器仍然接受了包括与

#include <iostream> 
using namespace std; 

这将导致std::max被带到全局命名空间,因此导致歧义替换它。将<iostream.h>替换为<iostream>或重命名您的max函数,问题应该消失。

编辑:你显然已经修复了包括,但我敢打赌,你仍然有using namespace std;某处。你需要摆脱这一点。事实上,你不应该在全球范围内使用using namespace

编辑:你可能也有using std::max的地方。你也需要摆脱它。

+0

这不是他的问题,即使添加'using namespace std'(在VS2008和GCC 4.1上测试),代码也可以编译。 – Manuel 2010-02-09 09:08:41

+1

如果还包括定义'std :: max'的,则不适用。如果您仔细阅读错误报告,您会发现编译器在处理用户头文件中定义的'max'和'algorithm'头文件中定义的'max'(正好在第1762行)中时遇到了困难。 – 2010-02-09 09:09:34

+0

不过,这应该不是一个错误,应该吗?标准中不保留':: max'这个名字。 – MSalters 2010-02-09 09:10:27

我不知道你正在使用的编译器,但第二个错误告诉你以下两个功能发生冲突:

::max 
std::max 

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

它说,被发现的

max<unsigned long> 

两个定义。一个定义在000001.hpp中,另一个定义在/ opt/aCC/include_std/algorithm中。编译器现在选择了000001.hpp中的那个,所以现在不存在错误。但它说这两个定义可能会在未来造成错误。

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