我试图在我的模板类中重载运算符,但编译器显示错误:
Error C2676 binary '++': 'Iterator' does not define this operator or a conversion to a type acceptable to the predefined operator
我无法找到问题的解决方案。这是我的班级:
#pragma once
template<typename T> class List;
template<typename T>
class Iterator
{
private:
friend class List<T>;
typename List<T> *pointer;
public:
Iterator(List<T>* mPointer)
{
pointer = mPointer;
}
void operator ++ ()
{
if (pointer != NULL)
{
pointer = pointer->next;
}
}
};
主要:
#include "stdafx.h"
#include "Lista.h"
#include <iostream>
using namespace std;
int main()
{
List<int> newList;
int x = 2;
newList.pushBack(x);
Iterator<int> iterator(newList.end());
cout << newList[iterator] << endl;
iterator++;
cin.get();
cin.get();
return 0;
}