Эта функция имеет значение:
Bucket & Bucket :: operator = (Bucket && otherBucket);
Я хочу позвонитьЭто.Однако я не могу и получаю ошибки:
Ошибка 'SortedList & SortedList :: operator = (const SortedList &)': попытка сослаться на удаленную функцию tests.h 25
Tests.h:
#include "sortedlist.h"
#include <iostream>
void runTests() {
cout << "***********************************************" << endl;
cout << "Testing sortedlist" << endl;
cout << "***********************************************" << endl;
cout << "Testing sort >>" << endl;
cout << "***********************************************" << endl;
vector<double> v =
{ 0.10, 0.90, 0.15, 0.95, 0.85,
0.73, 0.56, 0.32, 0.87, 0.11,
0.67, 0.34, 0.43, 0.61, 0.03 };
SortedList sortedListA;
cout << "BEFORE sortList function call: " << endl << endl;
sortedListA.testPrint();
sortedListA.sortList(v);
cout << "AFTER sortList function call: " << endl << endl;
sortedListA.testPrint();
cout << endl << "Using move assignmnet operator: " << endl << endl;
SortedList sortedListB;
cout << "Before move assignment operator call: " << endl << endl;
sortedListB.testPrint();
cout << "After move assignment operator call: " << endl << endl;
sortedListB = move(sortedListA); // This is where the error occurs!
}
sortedListB = move (sortedListA);// Здесь происходит ошибка!
Я пытаюсь использовать или вызвать оператор присваивания перемещения в моем классе.Тем не менее, это не так.SortedList наследует от класса Bucket и вот полный контекст :
Main.cpp
#include "tests.h"
#include "sortedlist.h"
#include <iostream>
int main()
{
runTests();
cout << endl;
system("Pause");
return 0;
}
Bucket.h
#ifndef BUCKET_H
#define BUCKET_H
#include <iostream>
using namespace std;
class Node
{
public:
Node() : item(0.0), link(nullptr) {}
Node(double newItem, Node *newLink) : item(newItem), link(newLink) {}
Node* getLink() const { return link; }
double getItem() const { return item; }
void setItem(double newItem) { item = newItem; }
void setLink(Node *newLink) { link = newLink; }
~Node() {}
private:
double item;
Node *link;
};
class Bucket
{
public:
Bucket();
void insert(double value);
void moveAppend(Bucket& otherBucket);
void testPrint() const;
void print() const;
bool isEmpty() const;
Bucket& operator=(Bucket&& otherBucket);
void deleteBucket();
~Bucket();
private:
Node *ptrToFirst;
Node *ptrToLast;
int numberOfElements;
};
#endif
Bucket.cpp: Вот где проблема.Перейдите к перегруженной функции оператора присваивания, я прокомментировал эту:
Bucket & Bucket :: operator = (Bucket && otherBucket);
#include "bucket.h"
Bucket::Bucket() {
ptrToFirst = nullptr;
ptrToLast = nullptr;
numberOfElements = 0;
}
void Bucket::insert(double value) {
if (numberOfElements != 0) {
Node *newNode = new Node(value, nullptr);
if (value < ptrToFirst->getItem()) {
newNode->setLink(ptrToFirst);
ptrToFirst = newNode;
}
else if (value > ptrToLast->getItem()) {
ptrToLast->setLink(newNode);
ptrToLast = newNode;
}
else if (value != ptrToFirst->getItem()) { // Prevents inserting duplicates of a value.
Node *current = ptrToFirst;
while (value > current->getLink()->getItem()) {
current = current->getLink();
}
if (current->getLink()->getItem() != value) { // Prevents inserting duplicates of a value.
newNode->setLink(current->getLink());
current->setLink(newNode);
}
}
}
else {
ptrToFirst = new Node(value, ptrToLast);
ptrToLast = ptrToFirst;
}
++numberOfElements;
}
void Bucket::moveAppend(Bucket& otherBucket) {
if (this != &otherBucket) {
if (numberOfElements == 0) {
delete ptrToFirst;
ptrToFirst = move(otherBucket.ptrToFirst);
otherBucket.ptrToFirst = nullptr;
delete ptrToLast;
ptrToLast = move(otherBucket.ptrToLast);
otherBucket.ptrToLast = nullptr;
numberOfElements = move(otherBucket.numberOfElements);
otherBucket.numberOfElements = move(0);
}
else {
Node *otherBucketFirstNode = move(otherBucket.ptrToFirst);
otherBucket.ptrToFirst = nullptr;
ptrToLast->setLink(otherBucketFirstNode);
ptrToLast = move(otherBucket.ptrToLast);
otherBucket.ptrToLast = nullptr;
numberOfElements = move(numberOfElements + otherBucket.numberOfElements);
otherBucket.numberOfElements = move(0);
}
}
else {
cerr << "Cannot append this bucket to the same bucket." << endl;
}
}
void Bucket::testPrint() const {
cout << "Pointer to first: " << ptrToFirst << endl;
cout << "Pointer to last: " << ptrToLast << endl;
if (ptrToFirst != nullptr && ptrToLast != nullptr) {
cout << "Value of ptrToFirst: " << ptrToFirst->getItem() << endl;
cout << "Value of ptrToLast: " << ptrToLast->getItem() << endl;
}
cout << "Number of elements: " << numberOfElements << endl;
cout << "Contents of bucket: " << endl;
Node *current = ptrToFirst;
while (current != nullptr) {
cout << current->getItem() << " ";
current = current->getLink();
}
cout << endl;
}
void Bucket::print() const {
Node *current = ptrToFirst;
while (current != nullptr) {
cout << current->getItem() << " ";
current = current->getLink();
}
}
bool Bucket::isEmpty() const {
return (numberOfElements == 0);
}
void Bucket::deleteBucket() {
Node *trailingCurrent;
while (ptrToFirst != nullptr) {
trailingCurrent = ptrToFirst;
ptrToFirst = ptrToFirst->getLink();
delete trailingCurrent;
trailingCurrent = nullptr;
}
ptrToLast = nullptr;
numberOfElements = 0;
}
Bucket& Bucket::operator=(Bucket&& otherBucket) {
cout << "Move assignment operator has been called." << endl;
// This is what I want it to call, but it's not calling it. Why?
return *this;
}
Bucket::~Bucket() {
deleteBucket();
}
SortedList.h:
#ifndef SORTEDLIST_H
#define SORTEDLIST_H
#include "bucketsort.h"
#include <vector>
using namespace std;
class SortedList : public Bucket
{
public:
SortedList();
void sortList(const vector <double>& list);
~SortedList();
private:
};
#endif
SortedList.cpp:
#include "sortedlist.h"
#include <iostream>
SortedList::SortedList() {}
void SortedList::sortList(const vector <double>& list) {
BucketSort bucketA;
bucketA.insert(list);
bucketA.createSortedList(*this);
}
SortedList::~SortedList() {}
BucketSort.h:
#ifndef BUCKETSORT_H
#define BUCKETSORT_H
#include "bucket.h"
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
const int DEFAULTCAPACITY = 10;
class BucketSort
{
public:
// Constructors:
BucketSort();
// Functions:
void print() const;
void insert(const vector <double>& v) const;
void createSortedList(Bucket& a);
//
~BucketSort();
private:
Bucket ** a;
};
#endif
BucketSort.cpp:
#include "bucketsort.h"
BucketSort::BucketSort() {
a = new Bucket*[DEFAULTCAPACITY]();
}
void BucketSort::print() const {
for (int i = 0; i < DEFAULTCAPACITY; ++i) {
if (a[i] != nullptr) {
a[i]->print();
}
}
}
void BucketSort::insert(const vector <double>& v) const {
int index;
for (int i = 0; i < v.size(); ++i) {
index = v[i] * 10;
if (a[index] == nullptr) {
Bucket* newBucket = new Bucket;
a[index] = newBucket;
}
a[index]->insert(v[i]);
}
}
void BucketSort::createSortedList(Bucket& thisBucket){
for (int i = 0; i < DEFAULTCAPACITY; ++i) {
if (a[i] != nullptr && !a[i]->isEmpty()) {
thisBucket.moveAppend(*a[i]);
}
}
}
BucketSort::~BucketSort() {
for (int i = 0; i < DEFAULTCAPACITY; ++i) {
if (a[i] != nullptr) {
a[i]->deleteBucket();
}
}
delete a;
a = nullptr;
}
Помощь?