简体   繁体   中英

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);

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);//

In my main i try to do:

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=MergeSort::sort(gooz);

//or even using it on an object of MergeSort won't work;

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.

inside static vector sort (vector a) its expecting object because sort is a static method while merge is not static. Try to call from sort using object or make merg as static

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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