【发布时间】:2011-01-18 13:35:26
【问题描述】:
我在简单的事情上需要帮助
我正在尝试创建类
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<class T> class merge_sort
{
protected:
vector<T> merge(const vector<T> &a, const vector<T> &b)
{
vector<T> v;
typename vector<T>::iterator A;
A= a.begin();
typename vector<T>::iterator B;
B= b.begin();
...
但是编译器给了我下一个错误:
no match for ‘operator=’ in ‘A = ((const std::vector<int, std::allocator<int> >*)a)->std::vector<_Tp, _Alloc>::begin [with _Tp = int, _Alloc = std::allocator<int>]()’ merge.cpp /merge_sort line 23 C/C++ Problem
【问题讨论】:
-
为什么在
vector<T>::iterator A;前面加上“typename”? -
@Kiril: 因为
vector<T>::iterator是从属名称? -
在下一行中,你只使用一次类型名
A,你会得到什么?此外,由于这是一个模板类,并且将位于头文件中 - 永远不要将任何using namespace声明放在头文件中。 -
@Mephane:模板定义有时属于
.cpp文件,如果模板仅用于此文件。 -
@Alexandre - 哎呀,我的错,对。 10x
标签: c++ templates stl iterator typename