2010-02-09 117 views
0

我是新來的模板在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請告訴我到底是什麼錯誤?

回答

1

你發佈的代碼編譯得很好,在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

7

你可能包括<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

1

我不知道你正在使用的編譯器,但第二個錯誤告訴你以下兩個功能發生衝突:

::max 
std::max 

這似乎很奇怪,你可能有一個using namespace std;某處或糟糕的是,其中一個包括使用iostream.h,如第一個錯誤中所述。你能提供關於你的編譯器/工具鏈和你的.hpp文件內容的更多信息嗎?

0

它說,被發現的

max<unsigned long> 

兩個定義。一個定義在000001.hpp中,另一個定義在/ opt/aCC/include_std/algorithm中。編譯器現在選擇了000001.hpp中的那個,所以現在不存在錯誤。但它說這兩個定義可能會在未來造成錯誤。

0

我不知道這是否會導致問題,但無論如何;你不應該爲你的全局(或本地)函數使用名稱max,因爲它是STL的一部分。