这是我正在处理的文件:
类.h
#include <vector>
using std::vector;
template<class T>
class test {
private:
vector<T> data;
public:
typedef vector<T> vt;
typedef typename vt::iterator it;
test() {
}
;
it find(T x);
}
和class.cpp
#include "class.h"
it test::find(T x) {
return find(data.begin(), data.end(), x);
}
如果我将 find 的实现放在类声明中,代码就可以工作。
但是,当我将实现与类分开时,编译器会报告错误“expected initializer before test”。
如何解决?问题是否与 typedef/typename 的范围有关?
抱歉我的英语不好,这是我的第二语言。
请指出我的代码中的任何错误以及我的英语
谢谢你的帮助。:D
最佳答案
当编译器看到 it
,它还不知道你的意思是 test<T>::it
.所以你必须告诉它:
template<class T> typename test<T>::it test<T>::find(T x) {
// The following line doesn't compile, but that's another issue:
// return find(data.begin(), data.end(), x);
}
参见 http://ideone.com/Rtho2用于工作程序。
关于c++ - 使用单独编译时返回 typedef 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8377656/