简体   繁体   English

没有对象就无法调用成员函数。 C ++

[英]Cannot call member function without object. C++

I'm making a merge sort class that has functions that take in a vector, sort it and return it. 我正在制作一个合并排序类,该类具有接受向量,对其进行排序并返回它的函数。 It looks like this: 看起来像这样:

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;

}



};

Issue i'm getting is when I try to do 我遇到的问题是当我尝试去做时

**FinalVec=merge(left,right); ** FinalVec =合并(左,右);

Error i'm getting is: 我得到的错误是:

error: cannot call member function 'std::vector MergeSort::merge(std::vector, std::vector) [with T = int]' without object FinalVec=merge(left,right);// 错误:无法调用成员函数'std :: vector MergeSort :: merge(std :: vector,std :: vector)[with T = int]'而没有对象FinalVec = merge(left,right); //

In my main i try to do: 在我的主要尝试中:

vector gooz; 矢量古兹

gooz.push_back(7); gooz.push_back(7);

gooz.push_back(5); gooz.push_back(5);

gooz.push_back(4); gooz.push_back(4);

gooz.push_back(3); gooz.push_back(3);

gooz.push_back(2); gooz.push_back(2);

gooz.push_back(1); gooz.push_back(1);

gooz=MergeSort::sort(gooz); gooz =归并排序::(gooz);

//or even using it on an object of MergeSort won't work; //甚至无法在MergeSort的对象上使用它;

Thanks! 谢谢!

might be because you have declared the 可能是因为您已经声明了

vector<T> merge (vector<T> left,vector<T> right)

as private so can not call from the Main method. 由于是私有的,因此无法从Main方法调用。

inside static vector sort (vector a) its expecting object because sort is a static method while merge is not static. 静态向量sort(向量a)中,它是期望对象,因为sort是静态方法,而merge不是静态的。 Try to call from sort using object or make merg as static 尝试使用对象从排序调用或将merg设为静态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM