【问题标题】:Invalid initialization of non-const reference of type 'AssortedSorter&' from an rvalue of type BubbleSort*"从 BubbleSort * 类型的右值初始化 'AssortedSorter&' 类型的非常量引用无效"
【发布时间】:2020-05-15 10:10:28
【问题描述】:

我正在编写一个程序,它有一个抽象基类“AssortedSorted”、一个派生类“BubbleSort”和一个用于测试排序的类“AssortedSorterTest”。

想法是创建一个 bubbleSort 实例,将该实例传递给 assortedSorterTest 的实例以及一个用于创建和排序的随机数数量的 int,如果向量已排序,则从 assortedsorter.testSort() 方法返回 true并且包含与给定向量相同数量的元素。

如果您阅读了代码,则需要更改一些内容才能完成此操作,但我不关心纠正这些内容,除非它们与我目前遇到的关于 main 中第 16 行无效初始化的问题有关.cpp。我得到的错误是这个

“从 BubbleSort* 类型的右值对 'AssortedSorter&' 类型的非常量引用的初始化无效”

最初我认为将#include "BubbleSort.h" 添加到 AssortedSorterTest 类可能会解决问题,但事实并非如此。我也尝试过更改一些对指针的引用,这给我带来了新的问题,所以我切换回了引用。我没有任何运气来解决这个问题,所以任何治疗都将不胜感激。

#pragma once
#include <vector>
#include <string>

class AssortedSorter
{
public:

    virtual std::vector<int> sort(const std::vector<int> &itemsToSort) = 0;
    virtual std::string getName() const = 0;
    virtual ~AssortedSorter() {};

};

#include <sstream>

class BubbleSort : public AssortedSorter
{    
private:
    long loopCount{0};
    long swapCount{0};

public:
    BubbleSort();
    ~BubbleSort() override;

    std::vector<int> sort(const std::vector<int> &itemsToSort) override;
    std::string getName() const override;
    friend std::ostream &operator<<(std::ostream &out, const BubbleSort &rhs);


};


#include "BubbleSort.h"

BubbleSort::BubbleSort()
{
}

BubbleSort::~BubbleSort() 
{
}

std::vector<int> BubbleSort::sort(const std::vector<int> &itemsToSort)
{

    std::vector<int> itemsSorted = itemsToSort;
    bool swap{false};
    int temporary_num{};

    do
    {
        swap = false;

        for (int index = 0; index < itemsSorted.size()-1; index++)
        {
            loopCount++;

            if (itemsSorted[index] > itemsSorted[index + 1])
            {
                swapCount++;

                temporary_num = itemsSorted[index];
                itemsSorted[index] = itemsSorted[index + 1];
                itemsSorted[index + 1] = temporary_num;
                swap = true;
            }
        }
    } while (swap);

    return itemsSorted;
}

std::string BubbleSort::getName() const
    {return "BubbleSort";}

//Overloaded insertion operator
std::ostream &operator<<(std::ostream &os, const BubbleSort &rhs)
{
    os << rhs.getName() << ":     " <<  std::to_string(rhs.loopCount) << "          " << std::to_string(rhs.swapCount);
    return os;
}


#pragma once
#include "AssortedSorter.h"
#include <vector>

class AssortedSorterTest
{
public:
    AssortedSorterTest();
    ~AssortedSorterTest();
    bool testSort(AssortedSorter &assortedSorter, int size);

};


#include "AssortedSorterTest.h"

AssortedSorterTest::AssortedSorterTest()
{
}

AssortedSorterTest::~AssortedSorterTest()
{
}

bool testSort(AssortedSorter &assortedSorter, int size)
{
    std::vector<int> randomNumbers;

    for(int index{0}; index < size; index++)
    {
        randomNumbers.push_back(rand());
    }

    std::vector<int> sortedVector = assortedSorter.sort(randomNumbers);

    if(sortedVector == randomNumbers)
    {
        return true;
    }

    else
    {
        return false;
    }
}
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include "AssortedSorterTest.h"
#include "BubbleSort.h"


std::vector<int> assign_vector_values(int size);

int main()
{

    std::vector<int> vec = assign_vector_values(100);

    AssortedSorter &bubbleSort = new BubbleSort; //problem is here

    AssortedSorterTest sortTester;

    if(sortTester.testSort(bubbleSort, 100))
    {
        std::cout << "Vector has been sorted" << std::endl;
    }

    else
    {
        std::cout << "Vector has not been sorted properly" << std::endl;
    }

    delete bubbleSort;

    return 0;
}


std::vector<int> assign_vector_values(int size)
{
    std::vector<int> temp_vector;

    for(int index{0}; index < size; index++)
    {

        temp_vector.push_back(rand());
    }

    return temp_vector;
}

【问题讨论】:

    标签: c++ vector derived-class abstract-base-class


    【解决方案1】:

    错误消息准确地告诉您问题所在。

    new BubbleSort 产生一个 指针 指向 BubbleSort

    您正在尝试将 reference 绑定到BubbleSort 的基类。那是行不通的。

    要么你需要取消引用指针,要么你需要用它初始化一个指针,而不是一个引用。

    在任何情况下,您都不应该在现代 C++ 中使用裸 new/delete。请改用std::unique_ptr&lt;AssortedSorter&gt;std::make_unique&lt;BubbleSort&gt;()

    std::unique_ptr<AssortedSorter> bubbleSort = std::make_unique<BubbleSort>();
    

    这需要#include&lt;memory&gt;


    或者,考虑到main 中的代码现在看起来如何,根本不需要动态分配。简单的

    BubbleSort bubbleSort;
    

    也可以。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 2021-10-26
      • 1970-01-01
      • 2017-09-08
      • 2015-01-26
      相关资源
      最近更新 更多