[英]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構建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;
}
我不記得標准中的確切措辭,但這里的簡化代碼演示了與您相同的行為:
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更寬松。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.