复制和分配构造函数的问题 [英] Problems with Copy and assignment constructor
问题描述
我有以下代码,用作链接列表的一部分:
I have the following code, used as a part of a Linked List:
// copy constructor:
LinkedList<T>(const LinkedList<T> &list)
{
// make a deep copy
for (LinkedList<T>::Iterator i = list.begin(); i != list.end(); i++)
{
add(*i);
}
}
// assignment constructor
LinkedList<T>& operator= (const LinkedList<T> &list)
{
// make a deep copy
for (LinkedList<T>::Iterator i = list.begin(); i != list.end(); i++)
{
add(*i);
}
}
但是在编译时出现以下错误(是当我将其用作分配构造函数时):
But when I compile I get the following errors (this is when I use it as an assignment constructor):
1>------ Build started: Project: AnotherLinkedList, Configuration: Debug Win32 ------
1>main.cpp
1>c:\users\ra\source\repos\sandbox\container\anotherlinkedlist\linkedlist.h(57): error C2662: 'LinkedList<int>::Iterator LinkedList<int>::begin(void)': cannot convert 'this' pointer from 'const LinkedList<int>' to 'LinkedList<int> &'
1>c:\users\ra\source\repos\sandbox\container\anotherlinkedlist\linkedlist.h(57): note: Conversion loses qualifiers
1>c:\users\ra\source\repos\sandbox\container\anotherlinkedlist\linkedlist.h(55): note: while compiling class template member function 'LinkedList<int> &LinkedList<int>::operator =(const LinkedList<int> &)'
1>c:\users\ra\source\repos\sandbox\container\anotherlinkedlist\main.cpp(20): note: see reference to function template instantiation 'LinkedList<int> &LinkedList<int>::operator =(const LinkedList<int> &)' being compiled
1>c:\users\ra\source\repos\sandbox\container\anotherlinkedlist\main.cpp(14): note: see reference to class template instantiation 'LinkedList<int>' being compiled
1>c:\users\ra\source\repos\sandbox\container\anotherlinkedlist\linkedlist.h(57): error C2662: 'LinkedList<int>::Iterator LinkedList<int>::end(void)': cannot convert 'this' pointer from 'const LinkedList<int>' to 'LinkedList<int> &'
1>c:\users\ra\source\repos\sandbox\container\anotherlinkedlist\linkedlist.h(57): note: Conversion loses qualifiers
1>Done building project "AnotherLinkedList.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
开始和结束的迭代器代码如下:
The iterator code for begin and end looks like this:
// get root
Iterator begin()
{
return Iterator(sp_Head);
}
// get end
Iterator end()
{
return Iterator(nullptr);
}
我怎么办?
推荐答案
根据错误消息,您的 LinkedList
似乎没有 begin()
和 end()
可以在const对象上调用。但是,复制构造函数和赋值运算符的参数是const。您必须添加 begin()
和 end()
的const版本。
Based on the error message, it would seem that your LinkedList
does not have variants of begin()
and end()
that can be called on a const object. The parameters to your copy constructor and assignment operator are const, however. You will have to add const versions of begin()
and end()
.
想必您正在寻找这样的东西:
Presumably, you're looking for something like this:
ConstIterator begin() const { Iterator(sp_Head); }
Iterator begin() { Iterator(sp_Head); }
ConstIterator end() const { ConstIterator(nullptr); }
Iterator end() { Iterator(nullptr); }
其中 ConstIterator
是您的版本迭代const元素的迭代器类型…
Where ConstIterator
is a version of your iterator type that iterates over const elements…
这篇关于复制和分配构造函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!