2013-10-28 34 views
2

我想編寫一個像「bind1st,bind2nd」這樣的函數來支持使用stl sort進行多字段排序。 不能使用std:綁定因爲我的G ++版本是3.4.5當使用自定義的「bind3rd」函數時,C++編譯時出現錯誤

它被稱爲是這樣的:

#include <alogritm> 
std::sort(begin(), end(), bind3rd(SortCond(), indicate)); 

我的模板函數是:

#ifndef __INCLUDE_TRIPLE_FUNCTION_H_ 
#define __INCLUDE_TRIPLE_FUNCTION_H_ 

#define TRIPLE_ARG(Operation, Type) Operation::Type 
// Define the triple_function prototype 
template<class Arg1, class Arg2, class Arg3, class Result> 
struct triple_function 
{ 
    // Define the argument type alias 
    typedef Arg1 first_argument_type; 
    typedef Arg2 second_argument_type; 
    typedef Arg3 third_argument_type; 
    typedef Result result_type; 
}; 

template <class Operation> 
class binder3rd : public binary_function<typename TRIPLE_ARG(Operation, first_argument_type), 
    typename TRIPLE_ARG(Operation, second_argument_type), typename TRIPLE_ARG(Operation, result_type)> 
{ 
protected: 
    Operation m_op; 
    typename Operation::third_argument_type value; 
public: 
    binder3rd(const Operation& x, const typename Operation::third_argument_type y):m_op(x), value(y){} 

    // Convert this function to binary_function using the third argment 
    typename Operation::result_type operator()(const typename Operation::first_argument_type& x, const typename Operation::second_argument_type& y) const 
    { 
     return m_op(x, y, value); 
    } 

}; 

// bind3rd function implementation 
template<class Operation, class Arg> 
inline binder3rd<Operation> bind3rd(const Operation& fn, const Arg& x) 
{ 
    return binder3rd<Operation>(fn, x); 
} 

#endif //__INCLUDE/TRIPLE_FUNCTION_H_ 
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */ 

而且我的測試CPP文件是:

#include <algorithm> 
#include <cstdlib> 

#include "triple_function.h" 

using namespace std; 

class TestClass{ 
public: 
    int arr[16]; 

    TestClass() { 
     for (int i = 0; i < 16; ++i) { 
      arr[i] = rand() % 100; 
     } 
    } 
}; 

// sort by which fields 
class Indicate { 
public: 
    int ind[16]; 
}; 


struct SortA : public triple_function < TestClass, TestClass, const Indicate, bool > 
{ 
    bool operator() (const TestClass& a, const TestClass& b, 
      const Indicate& indicate) const 
    { 
     for (int i = 0; i < 16; ++i) { 
      int pos = indicate.ind[i]; 
      if (a.arr[pos] == b.arr[pos]) { 
       continue ; 
      } 
      return a.arr[pos] < b.arr[pos]; 
     } 
     return false; 
    } 
}; 


int main() { 
    TestClass a[10]; 

    Indicate ind; 
    for(int i = 0; i < 16; ++i) { 
     ind.ind[i] = i; 
    } 

    sort(a, a+10, bind3rd(SortA(), ind)); 
    // bind3rd(SortA, ind); 
} 

當使用g ++編譯時,我得到了這些com堆錯誤: 我的代碼有什麼問題?

In file included from test.cpp:19: 
triple_function.h:32: error: expected template-name before '<' token 
triple_function.h:32: error: expected `{' before '<' token 
triple_function.h:32: error: expected unqualified-id before '<' token 
test.cpp: In function `int main()': 
test.cpp:62: error: invalid use of undefined type `class binder3rd<SortA>' 
triple_function.h:32: error: declaration of `class binder3rd<SortA>' 
triple_function.h: In function `binder3rd<Operation> bind3rd(const Operation&, const Arg&) [with Operation = SortA, Arg = Indicate]': 
test.cpp:62: instantiated from here 
triple_function.h:52: error: return type `class binder3rd<SortA>' is incomplete 
triple_function.h:53: error: invalid use of undefined type `class binder3rd<SortA>' 
triple_function.h:32: error: declaration of `class binder3rd<SortA>' 
+1

您可能想查看['std :: bind'](http://en.cppreference.com/w/cpp/utility/functional/bind)。 bind1st等已被棄用(請參閱[例如[this reference](http://en.cppreference.com/w/cpp/utility/functional))。 –

回答

1

編譯器正在跳過你的基類binary_function。如果您的意思是std::binary_function,那麼#include <functional>std::前綴。當然,因爲你實際上並沒有定義二進制函數,所以我不確定基類是否合適......

正如其他人所說的,如果你可以使用C語言,std::bind是更好的解決方案++ 11。

+0

謝謝!我知道了! – wangjild

1

改爲使用std::bind,例如,像這樣:

#include <functional> 

... 

bool my_compare(const TestClass& a, const TestClass& b, 
       const Indicate& indicate) 
{ 
    ... 
} 

int main() 
{ 
    ... 

    using namespace std::placeholders; 
    std::sort(a, a+10, std::bind(my_compare, _1, _2, ind)); 
} 
+0

我的g ++版本是3.4.5 – wangjild

相關問題