无法使用类型“ SortedList”的值来初始化类型“ Bucket&”的引用(非const限定)。如何解决此错误?

时间:2018-10-11 02:44:54

标签: c++ sorting

该错误在函数方法sortList的SortedList类中发生。我试图重命名,删除,移动和操纵某些东西而无济于事。我将向您展示大量代码,以便您了解我的来源。

void SortedList::sortList(const vector <double>& list) {
    BucketSort bucketA;
    bucketA.insert(list);
    bucketA.createSortedList(*this); // <--- Problem here, on this one line. 
                                    // The * is highlighted in red in my IDE.
}

错误:无法使用类型为“ SortedList”的值初始化“ Bucket&”类型的引用(非const限定)

作为参考,如果您需要查看有关我的错误的信息:

SortedList.h:

#ifndef SORTEDLIST_H
#define SORTEDLIST_H    

#include "bucketsort.h"
#include <vector>

using namespace std;

class SortedList
{
public:
    SortedList();
    void sortList(const vector <double>& list);
    ~SortedList();
private:
};

#endif

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& a){

}

BucketSort::~BucketSort() {
    for (int i = 0; i < DEFAULTCAPACITY; ++i) {
        if (a[i] != nullptr) {
            a[i]->deleteBucket();
        }
    }

    delete a;
    a = nullptr;
}

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 testPrint() const;
    void print() const;
    int getNumberOfElements() const;
    void deleteBucket();
    ~Bucket();
private:
    Node * ptrToFirst;
    Node *ptrToLast;
    int numberOfElements;
};

#endif

Bucket.cpp:

#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()) {
            Node *current = ptrToFirst;

            while (value > current->getLink()->getItem()) {
                current = current->getLink();
            }

            if (current->getLink()->getItem() != value) { 
                newNode->setLink(current->getLink());
                current->setLink(newNode);
            }
        }
    }
    else {
        ptrToFirst = new Node(value, ptrToLast);
        ptrToLast = ptrToFirst;
    }

    ++numberOfElements;
}

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();
    }
}

int Bucket::getNumberOfElements() const {
    return numberOfElements;
}

void Bucket::deleteBucket() { 
    Node *trailingCurrent;

    while (ptrToFirst != nullptr) {
        trailingCurrent = ptrToFirst;
        ptrToFirst = ptrToFirst->getLink();
        delete trailingCurrent;
        trailingCurrent = nullptr;
    }

    ptrToLast = nullptr;
    numberOfElements = 0;
}

Bucket::~Bucket() {
    deleteBucket();
}

帮助?

1 个答案:

答案 0 :(得分:1)

void SortedList::sortList(const vector <double>& list) {
   [...]
   bucketA.createSortedList(*this); // <--- Problem here

在上一行中,this的类型为SortedList *,因此*this的类型为SortedList &

...但是,您的createdSortedList方法需要使用其他类型的参数Bucket &

void createSortedList(Bucket& a);

由于SortedListBucket的类型不同,并且(编译器知道)无法将SortedList对象转换为{{1} }对象,编译器正确地将调用标记为错误。

要解决此问题,您需要更改Bucket以使用createSortedList作为参数,而不是SortedList &,或者更改您的呼叫以传递{{1 }},而不是Bucket &