Xcode7: 基本 C++ 程序中的未知类型名称错误

Xcode7: unknown type name error in a basic C++ program

我正在使用最新版本的 Xcode 编写有关 STL 的 C++ 程序,但出现错误 "Unknown type name 'ubResultData'" 和 "Unknown type name 'ubFirstArgument'"。我尝试用标准unary_function和binary_function重写程序,错误依旧。然后,我用 VS2010 和 VS2013 构建程序,并成功构建。程序有什么问题?

错误在classbinder2ND的最后一行。

#include <iostream>
#include <vector>
using namespace std;

template<typename InputIterator, typename Predicator>
inline int countIF(InputIterator First, InputIterator Last, Predicator Pred){
    int count = 0;
    for (; First != Last; ++First) {
        if(Pred(*First))++count;
    }

    return count;
}

template<typename Arg1, typename Result>
struct UnaryBase{
    typedef Arg1 ubFirstArgument;
    typedef Result ubResultData;
};

template<typename Arg1, typename Arg2, typename Result>
struct BinaryBase{
    typedef Arg1 bbFirstArgument;
    typedef Arg2 bbSecondArgument;
    typedef Result bbResultData;
};

template<typename T>
struct LESS:public BinaryBase<T, T, bool>{
    bool operator()(const T& Left, const T& Right)const{
        return (Left < Right);
    }
};

template<typename BinOP>
class binder2ND:public UnaryBase<typename BinOP::bbFirstArgument, typename BinOP::bbResultData>{
protected:
    BinOP OP;
    typename BinOP::bbSecondArgument Arg2;
public:
    binder2ND(const BinOP& Oper, const typename BinOP::bbSecondArgument& valRight):OP(Oper),Arg2(valRight){}
    ubResultData operator()(const ubFirstArgument &ubArg1)const{return OP(ubArg1,Arg2);}
};

template<typename BinOP, typename rightVal>
binder2ND<BinOP> bind2ND(const BinOP& OP, const rightVal& vRight){
    return binder2ND<BinOP>(OP, vRight);
}

int main(){
    vector<int> myVec;
    for (int i = 0; i < 50; ++i) {
        myVec.push_back(rand()%100);
    }

    int countNUm = countIF(myVec.begin(), myVec.end(), bind2ND(LESS<int>(), 30));
    cout << "Numbers=" << countNUm << endl;

    return 0;
}

我不记得标准中的确切措辞,但这是演示与您的相同行为的简化代码:

template <typename T> struct A {
  typedef T X;
};

template <typename T> struct B: public A<T> {
  X x(void) { return 5; }
};

下面是编译没有任何错误的版本:

template <typename T> struct A {
  typedef T X;
};

template <typename T> struct B: public A<T> {
  typename A<T>::X x(void) { return 5; }
};

所以基本上,您需要准确指定此类型的来源。

看起来 VS* 编译器比 gcc 或 clang 更宽松一些。 :)