我正在创建一个合并排序类,它具有接收向量的函数,对其进行排序并返回它。 它看起来像这样:
class MergeSort {
public:
template <class T>
static vector<T> sort (vector<T> a)
{
if (a.size()<=1)
{
return a;
}
else
{
//some code here(seperating the vector
vector<T> left=sort(leftVec);//recursive call
vector<T> right=sort(rightVec);//recursive call
vector<T>FinalVec;//this will be the final vector that is returned
FinalVec=merge(left,right);//will merge and sort when all the vectors
//getting issues here^^^^^
return FinalVec;
}
}
private:
template <class T>
vector<T> merge (vector<T> left,vector<T> right)
{
//some code here
return final;
}
};
我正在尝试做的事情
** FinalVec =合并(左,右);
我得到的错误是:
错误:无法调用成员函数'std :: vector MergeSort :: merge(std :: vector,std :: vector)[with T = int]' 没有对象 FinalVec =合并(左,右); //
在我的主要部分我尝试做:
vector gooz;
gooz.push_back(7);
gooz.push_back(5);
gooz.push_back(4);
gooz.push_back(3);
gooz.push_back(2);
gooz.push_back(1);
gooz =归并排序::(gooz);
//甚至在MergeSort的对象上使用它都不会起作用;
谢谢!
答案 0 :(得分:1)
可能是因为你宣布了
vector<T> merge (vector<T> left,vector<T> right)
为私有,因此无法从主方法调用。
在静态向量排序(向量a)内部,因为排序是一种静态方法,而合并不是静态的。尝试使用object从排序调用或将 merg 设为静态