2017-08-29 72 views
-3
template<class argument1, class argument2, class result> 
class binary_function{ 
    public: 
    typedef argument1 first_argument_type; 
    typedef argument2 second_argument_type; 
    typedef result result_type; 
}; 
class cmplxData : binary_function<double, double, int>{ 
    public: 
      result_type operator()(first_argument_type t1, second_argument_type t2){ 
        return (result_type)(t1+t2); 
      } 
}; 

int main(int argc, char *argv[]){ 
    return 0; 
} 

拋出下面的錯誤下面輸入的代碼不編譯

error: reference to ‘binary_function’ is ambiguous prog18.cpp:38:34: error: expected template-name before ‘<’ token class cmplxData : binary_function{
^prog18.cpp:38:34: error: expected ‘{’ before ‘<’ token prog18.cpp:38:34: error: expected unqualified-id before ‘<’ token

+7

[無法重現](http://coliru.stacked-crooked.com/a/d51c560a75013858)。 – Angew

+0

這款編譯器適合VS2015。什麼編譯器和你使用什麼標誌? –

+7

38行應該在你的17行代碼中?請提供[MCVE] – muXXmit2X

回答

1

你的類模板binary_function產生,因爲它是在STD已定義的模糊性,std::binary_function

您可以添加使用您的自定義binary_function在另一個命名空間,使用完全限定命名空間,或只使用std::binary_function定義。

+0

OPs'binary_function'在另一個命名空間,全球之一,而'std :: binary_function'在標準命名空間 – user463035818

+0

@ tobi303你是對的! – Rama

2

好吧,考慮到評論的信息,你在做兩件事情:

  1. 你包含一個頭文件的地方,包括std::binary_function

  2. 定義你也using namespace std

這會導致不確定性,因爲編譯器的binary_function和之間不能區分現在也可以稱爲binary_function。這就是爲什麼你不應該使用using namespace anywhere

一個解決將是的cmplxData類聲明改寫爲

class cmplxData : ::binary_function<double, double, int>{ 

通過這個您使用::binary_function::std::binary_function和消除不確定性。您明確調用在全局名稱空間處聲明的binary_function