C++ 类型为';的非常量引用的初始化无效;分类分拣机';来自类型为BubbleSort*”的右值;
我有一个我正在编写的程序,它有一个抽象基类“SockedSorted”,一个派生类“BubbleSort”,还有一个测试排序的类“SockedSorteTest” 其思想是创建一个bubbleSort实例,将该实例与要创建和排序的随机数数量的int一起传递给SorterTest实例,如果向量已排序且包含与给定向量相同数量的元素,则从SorterTestSort()方法返回true 如果您阅读了代码,则需要对某些内容进行更改以实现此目的,但我并不关心如何更正这些内容,除非它们与我当前在main.cpp第16行遇到的关于无效初始化的问题相关。我得到的错误是 “从BubbleSort*类型的右值初始化'SordedSorter&'类型的非常量引用无效” 起初,我认为在分类排序测试类中添加#include“BubbleSort.h”可以纠正这个问题,但事实并非如此。我还尝试将一些引用更改为指针,这给我带来了新的问题,所以我切换回引用。我还没弄明白这件事,所以任何一个小家伙都会很感激的C++ 类型为';的非常量引用的初始化无效;分类分拣机';来自类型为BubbleSort*”的右值;,c++,vector,derived-class,abstract-base-class,C++,Vector,Derived Class,Abstract Base Class,我有一个我正在编写的程序,它有一个抽象基类“SockedSorted”,一个派生类“BubbleSort”,还有一个测试排序的类“SockedSorteTest” 其思想是创建一个bubbleSort实例,将该实例与要创建和排序的随机数数量的int一起传递给SorterTest实例,如果向量已排序且包含与给定向量相同数量的元素,则从SorterTestSort()方法返回true 如果您阅读了代码,则需要对某些内容进行更改以实现此目的,但我并不关心如何更正这些内容,除非它们与我当前在main.c
#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() {};
};
#pragma一次
#包括
#包括
分类分拣机
{
公众:
虚拟std::vector sort(const std::vector&itemsToSort)=0;
虚拟std::string getName()const=0;
虚拟~分类分拣机(){};
};
#包括
BubbleSort类:公共分类分拣机
{
私人:
长循环计数{0};
长swapCount{0};
公众:
泡泡运动();
~BubbleSort()覆盖;
std::vector sort(const std::vector和itemsToSort)覆盖;
std::string getName()常量覆盖;
friend std::ostream&operator错误消息会准确地告诉您问题所在
new BubbleSort
生成指向BubbleSort
的指针
您正试图将对BubbleSort
基类的引用绑定到它。这无法工作
您需要取消对指针的引用,或者需要使用它初始化指针,而不是引用
<>无论如何,在现代C++中,不应该使用裸代码>新的< /代码> /<代码>删除< /代码>。使用<代码> STD::UngQuyPPTR < /C>和<代码> STD::MaMuxUngor()/<代码>:
std::unique_ptr<AssortedSorter> bubbleSort = std::make_unique<BubbleSort>();
也可以
#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;
}
std::unique_ptr<AssortedSorter> bubbleSort = std::make_unique<BubbleSort>();
BubbleSort bubbleSort;