gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:23 25 4
gpt4 key购买 nike

我正在使用最新版本的 Xcode 开发一个关于 STL 的 C++ 程序,我收到错误“Unknown type name 'ubResultData'”和“Unknown type name 'ubFirstArgument'”。我尝试用标准的 unary_function 和 binary_function 重写程序,错误依旧。然后,我用 VS2010 和 VS2013 构建程序,并成功构建。程序有什么问题?

错误在 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 更宽松一些。 :)

关于c++ - Xcode7:基本 C++ 程序中的未知类型名称错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36710579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com