【发布时间】:2011-06-25 22:23:10
【问题描述】:
我创建了一个 Chromosome 类,它最终只是一个带有 ostream 运算符的向量包装器,因此我决定改为 typedef 向量。但是,我在使用模板化 ostream 运算符时遇到了问题……这是最好的方法吗? (我已经看到了一些方法,但都没有成功)
template<typename G>
class Chromosome {
public:
typedef typename std::vector<G> type;
typedef typename std::pair<type *,type *> ptr_pair;
};
template<typename G> //line 19 below:
std::ostream& operator<<(std::ostream& os, const Chromosome<G>::type& chromosome) {
for(auto iter = chromosome.begin(); iter != chromosome.end(); ++iter)
std::cout << *iter;
return os;
}
目前我得到的错误是:
chromosome.h:19: error: expected unqualified-id before ‘&’ token
chromosome.h:19: error: expected ‘)’ before ‘&’ token
chromosome.h:19: error: expected initializer before ‘&’ token
干杯。
【问题讨论】:
标签: c++ templates vector typedef ostream