尝试返回矢量时收到错误"E0415"
Receiving error 'E0415' when trying to return a vector
试图在函数完成后返回一个完整的气泡排序,我得到这个:
E0415 不存在合适的构造函数来从"std::vector
, std::allocator> *" 转换为 "std::vector<double,>>">
这是代码
class BubbleSort : SortingAlogrithm
{
void swap(double *xp, double *yp)
{
double temp = *xp;
*xp = *yp;
*yp = temp;
}
public:
vector<double> Sort(vector<double> &newVect, int arraySize)
{
cout << "Bubble sort algorithm commencing" << endl;
int i, j;
for (i = 0; i < arraySize - 1; i++)
// Last i elements are already in place
for (j = 0; j < arraySize - i - 1; j++)
if (newVect[j] > newVect[j + 1])
swap(&newVect[j], &newVect[j + 1]);
cout << "Ordered List: ";
for (int i = 0; i < arraySize; i++)
{
cout << newVect[i] << " ";
}
return &newVect;
}
};
return &newVect;
语法不正确,因为返回类型为 std::vector<double>
,而 &newVect
的类型为 std::vector<double>*
。
这就是编译器抱怨的。
您需要使用
return newVect;
改进建议
最好将返回类型更改为引用,这样就不会强制调用函数在调用函数时创建副本。
vector<double>& Sort(vector<double> &newVect, int arraySize)
{
...
return newVect;
}
最好还是将返回类型更改为void
,因为调用函数对对象进行了排序。
void Sort(vector<double> &newVect, int arraySize)
{
...
// Not return statement
}
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Opengl 精度转换错误编译错误 E0415
- 尝试返回矢量时收到错误"E0415"