[英]Trying to write a std::iterator : Compilation error
I am trying to write an std::iterator
for the CArray<Type,ArgType>
MFC class. 我正在尝试为
CArray<Type,ArgType>
MFC类编写一个std::iterator
。 This is what I have done till now: 这是我到目前为止所做的:
template <class Type, class ArgType>
class CArrayIterator : public std::iterator<std::random_access_iterator_tag, ArgType>
{
public:
CArrayIterator(CArray<Type,ArgType>& array_in, int index_in = 0)
: m_pArray(&array_in), m_index(index_in)
{
}
void operator++() { ++m_index; }
void operator++(int) { ++m_index; }
void operator--() { --m_index; }
void operator--(int) { --m_index; }
void operator+=(int n) { m_index += n; }
void operator-=(int n) { m_index -= n; }
typename ArgType operator*() const{ return m_pArray->GetAt(m_index); }
typename ArgType operator->() const { return m_pArray->GetAt(m_index); }
bool operator==(const CArrayIterator& other) const
{
return m_pArray == other.m_pArray && m_index == other.m_index;
}
bool operator!=(const CArrayIterator& other) const
{
return ! (operator==(other));
}
private:
CArray<Type,ArgType>* m_pArray;
int m_index;
};
I also provided two helper functions to create the iterators like this: 我还提供了两个帮助器函数来创建像这样的迭代器:
template<class Type, class ArgType>
CArrayIterator<Type,ArgType> make_begin(CArray<Type,ArgType>& array_in)
{
return CArrayIterator<Type,ArgType>(array_in, 0);
}
template<class Type, class ArgType>
CArrayIterator<Type,ArgType> make_end(CArray<Type,ArgType>& array_in)
{
return CArrayIterator<Type,ArgType>(array_in, array_in.GetSize());
}
To test the code, I wrote a simple class A
and tried to use it like this: 为了测试代码,我编写了一个简单的
class A
并尝试如下使用它:
class A
{
public:
A(int n): m_i(n)
{
}
int get() const
{
return m_i;
}
private:
int m_i;
};
struct Test
{
void operator()(A* p)
{
std::cout<<p->get()<<"\n";
}
};
int main(int argc, char **argv)
{
CArray<A*, A*> b;
b.Add(new A(10));
b.Add(new A(20));
std::for_each(make_begin(b), make_end(b), Test());
return 0;
}
But when I compile this code, I get the following error: 但是,当我编译此代码时,出现以下错误:
Error 4 error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'CArrayIterator' C:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\include\\xutility 1564 Vs8Console
错误4错误C2784:'bool std :: operator <(const std :: _ Tree <_Traits>&,const std :: _ Tree <_Traits>&)':无法推断'const std :: _ Tree <_Traits>的模板参数&'从'CArrayIterator'C:\\ Program Files \\ Microsoft Visual Studio 9.0 \\ VC \\ include \\ xutility 1564 Vs8Console
Can anybody throw some light on what I am doing wrong and how it can be corrected? 任何人都可以对我做错了什么以及如何纠正它有所了解吗? I am using VC9 compiler if it matters.
如果有问题,我正在使用VC9编译器。
You've said that your iterator is a "random access iterator". 您已经说过,您的迭代器是“随机访问迭代器”。 Part of the requirements for random access iterators is that you provide
<
, <=
, >
and >=
comparision operators, with <
giving a strict weak ordering and the usual relationships between them. 随机访问迭代器的部分要求是您提供
<
, <=
, >
和>=
比较运算符,其中<
给出严格的弱排序及其之间的通常关系。
You need to provide the appropriate comparison operators, or you could consider 'downgrading' to a bi-directional iterator. 您需要提供适当的比较运算符,否则您可以考虑“降级”为双向迭代器。
Depending on what you're trying to do, you may not need to write an iterator class at all. 根据您要执行的操作,可能根本不需要编写迭代器类。
CArray
s are similar to vector
s in that the underlying data store is just a C-style array and the class manages allocation & deallocation for you. CArray
与vector
相似,因为基础数据存储区只是一个C样式的数组,并且该类为您管理分配和释放。 You can use GetData()
to get a pointer to the data itself, and use simple pointer math to find the end; 您可以使用
GetData()
获取指向数据本身的指针,并使用简单的指针数学来查找结尾; much the same way you would use raw C-style arrays with the STL <algorithm>
s. 与将原始C样式数组与STL
<algorithm>
一起使用的方式几乎相同。 To wit: 以机智:
#define _AFXDLL
#include <afx.h>
#include <afxwin.h> // MFC core and standard components
#include <afxtempl.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
CArray<int, int> ints;
srand((unsigned)time(0));
for( int i = 0; i < 10; ++i )
ints.Add(rand()%10);
vector<int> ints2;
copy(ints.GetData(), ints.GetData()+ints.GetCount(), back_inserter(ints2));
cout << "Original : ";
copy(ints.GetData(), ints.GetData()+ints.GetCount(), ostream_iterator<int>(cout, " "));
cout << endl
<< endl
<< "Copy : ";
copy(ints2.begin(), ints2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
return 0;
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.