如何重载间接运算符? (C++)

发布于 2024-07-21 08:00:44 字数 964 浏览 6 评论 0原文

我正在尝试创建一个迭代器类作为列表类的成员类,并尝试重载间接运算符 (*) 以访问它指向的列表:

template<class T>
T list<T>::iterator::operator*(iterator& iter)
{
    return ((iter.lstptr)->current)->data;
}

其中 lstptr 是一个指针对于列表来说,current是一个指向节点类的指针,节点类包含T类型的数据成员data

迭代器是这样声明的:

template<class T>
class list
{
public:
class iterator;
};

template<class T>
class list<T>::iterator
{
//stuff
};

我能够很好地编译重载运算符*的函数定义,但是当我尝试执行以下操作时:

list<int> lst1;
lst1.add(6);
list<int>::iterator IT;
IT = lst1;
//everything above this point compiles fine
int a = *IT; //error here (line fourteen)

我得到的错误是 <1> 我正在使用非法间接寻址,并且 <2> 它无法从 list::iterator 转换为 int。 这两个错误都发生在第十四行。

有谁知道我做错了什么以及如何正确重载间接运算符?

注意:如果您需要查看更多代码,请告诉我哪一部分,因为我不想将整个代码放在这里,因为它大约有 205 行,其中 204 行(我认为)没有任何错误。

I'm trying to create an iterator class as a member-class for a list class, and am trying to overload the indirection operator (*) to access the list it's pointing to:

template<class T>
T list<T>::iterator::operator*(iterator& iter)
{
    return ((iter.lstptr)->current)->data;
}

where lstptr is a pointer to a list, current is a pointer to a node class, and the node class contains the data member data of type T.

Iterator is declared like this:

template<class T>
class list
{
public:
class iterator;
};

template<class T>
class list<T>::iterator
{
//stuff
};

I am able to compile the function definition of the overloaded operator* fine, but when I try to do something like:

list<int> lst1;
lst1.add(6);
list<int>::iterator IT;
IT = lst1;
//everything above this point compiles fine
int a = *IT; //error here (line fourteen)

The error I get says <1> that I am using an illegal indirection, and <2> that it cannot convert from list::iterator to int. Both errors occur on line fourteen.

Does anybody know what I am doing wrong and how I can overload the indirection operator correctly?

NB: If you need to see more code, tell me which part, because I don't want to put th entire code up here because it's abot 205 lines, and 204 of those lines don't (I think) have any errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美羊羊 2024-07-28 08:00:44

您重载了乘法运算符。 取出参数使其成为间接运算符。

template<class T>
T list<T>::iterator::operator*()
{
    return ((this->lstptr)->current)->data;
}

如果您希望编译像 *IT = 3; 这样的代码,您还应该让它返回一个引用。

template<class T>
T& list<T>::iterator::operator*()
{
    return ((this->lstptr)->current)->data;
}

You overloaded the multiply operator. Take the parameter out to make it an indirection operator.

template<class T>
T list<T>::iterator::operator*()
{
    return ((this->lstptr)->current)->data;
}

You should also have it return a reference if you want code like *IT = 3; to compile.

template<class T>
T& list<T>::iterator::operator*()
{
    return ((this->lstptr)->current)->data;
}
一袭白衣梦中忆 2024-07-28 08:00:44

你这里有两个问题; 第一个是您不小心重载了乘法运算符而不是解引用运算符; 第二是你没有返回引用类型。

第一个问题是由参数数量引起的。 类的每个非静态成员函数都有一个附加的“隐藏”参数:thisthis 当然,是指向正在调用函数的对象的指针。 因此,您实际上声明了带有两个参数的运算符的版本。 通过删除第二个迭代器参数并对 this 进行操作,您将重载一元 * 而不是二进制的。

第二个问题是返回类型的一个小问题; 您将返回原始对象的副本,而不是原始对象本身。 将返回类型声明为 T& 以返回引用。

You have two problems here; the first is that you have accidentally overloaded the multiplication operator and not the dereferencing operator; the second is that you haven't returned a reference type.

The first issue comes about as a result of the number of parameters. Every non-static member function of a class has an additional "hidden" parameter: this. this is, of course, the pointer to the object the function is being invoked on. As a result, you have actually declared a version of the operator taking two parameters. By removing the second iterator parameter and operating on this, you will be overloading the unary * and not the binary one.

The second issue is a minor one of return type; you are returning a copy to the original object and not the original object itself. Declare the return type as T& to return a reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文