我正在使用最新版本的Xcode开发一个关于STL的C ++程序,我收到错误“Unknown type name'ubResultData'”和“Unknown type name'ubFirstArgument'”。我试图用标准的unary_function和binary_function重写程序,错误保持不变。然后,我使用VS2010和VS2013构建progaram,并且它已成功构建。该计划有什么问题?
错误位于类binder2ND的最后一行。
#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;
}
答案 0 :(得分: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更宽松一些。 :)