Reference to Non-Static Member Function Must Be Called

Reference to non-static member function must be called

The problem is that buttonClickedEvent is a member function and you need a pointer to member in order to invoke it.

Try this:

void (MyClass::*func)(int);
func = &MyClass::buttonClickedEvent;

And then when you invoke it, you need an object of type MyClass to do so, for example this:

(this->*func)(<argument>);

http://www.codeguru.com/cpp/cpp/article.php/c17401/C-Tutorial-PointertoMember-Function.htm

Getting the Error reference to non-static member function must be called

You cannot pass a non-static member function to std::sort in that way because it would need an instance of Solution to call it on.

Fortunately, the solution in this case is simple: since sortbysec doesn't reference any member variables, you can declare it static:

static bool sortbysec(const pair<int,int> &a,
const pair<int,int> &b)

If you do need to call a non-static member function as your sort function, you can do it via a capturing lambda:

sort(zeros.begin(),zeros.end(),
[this] (const pair<int,int> &a, const pair<int,int> &b)
{ return sortbysec (a, b); });

Error: reference to non static member function must be called not getting resolved when using classes

The sort algorithm in your case needs "function object" (instance of object having operator() or pointer to function), which accepts references to 2 comparing vectors and returns bool, for example:

// function out of class scope
bool compVectorBySecondElt(const vector<int>& first, const vector<int>& second) {
return first[1] < second[1]; // hope the index 1 is valid
}

As stated in reply above, your comparator could be "free function, a static member function, or a lambda". Also it could be object of class like:

struct MyVectorCmp {
bool operator()(const vector<int>& first, const vector<int>& second) {
return first[1] < second[1]; // hope the index 1 is valid
}
};

The common side of all these cases is ability to call your functional object with 2 parameters and to accept bool value as result.

In your example you accepted the error because any non-static method of class is not a regular function, besides explicitly specified parameters it could be considered as having additional implicit this parameter, pointing to current object.

Non-type parametric template function, error: reference to non-static member function must be called

The problem with your code is that a template argument has to be compile-time constant. For a function call this means it has to be constexpr while for a class method this means it has to be static.

You have only two options:

  • Either you leave the configuration of the Mode to run-time and allow the user to potentially re-configuring the Mode. For this you will have to introduce an if/else or switch statement inside the function.

    class Foo {
    public:
    constexpr Foo(double const x = 1.0, Mode const y = Mode::ka) noexcept
    : x_{x}, y_{y} {
    return;
    }
    Mode get() const noexcept {
    return y_;
    }
    double FooFunc() const {
    switch (y_) {
    case Mode::ka:
    return 1.0/x_;
    case Mode::kb:
    return 1.0/x_/x_;
    case Mode::kab:
    return 1.0/x_/x_/x_;
    default:
    // Throw error
    }
    }
    private:
    double x_;
    Mode y_;
    };
  • If you do not plan to give the user the possibility to change the Mode on the fly you might make it a class template and specialise the corresponding function. This way there is though no possibility to change the mode on the fly.

    template <Mode M = Mode::ka>
    class Foo {
    public:
    constexpr Foo(double const x = 1.0) noexcept
    : x_(x) {
    return;
    }
    static constexpr Mode get() noexcept {
    return M;
    }
    double FooFunc() const noexcept;
    private:
    double x_;
    Mode y_;
    };

    template<>
    inline double Foo<Mode::ka>::FooFunc() const noexcept {
    return 1.0/x_;
    }
    template<>
    inline double Foo<Mode::kb>::FooFunc() const noexcept {
    return 1.0/x_/x_;
    }
    template<>
    inline double Foo<Mode::kab>::FooFunc() const noexcept {
    return 1.0/x_/x_/x_;
    }

How do i fix these errors : reference to non-static member function must be called and invalid use of member 'mat' in static member function?

The trick is to define a call-operator on your Solution class so that it can be used as a Comparator object that is passed to the std::sort() call, like this:

#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
int mat[26][26];

bool comp(char a, char b, int i) const {
if (i == 26) {
return a<b;
}
if (mat[a-'A'][i] > mat[b-'A'][i]) {
return true;
}
else if (mat[a-'A'][i] < mat[b-'A'][i]) {
return false;
}
return comp(a,b,i+1);
}

bool operator()(const char & a, const char & b) const
{
return comp(a, b, 0);
}
};

int main(int argc, char ** argv)
{
std::vector<char> str;
str.push_back('d');
str.push_back('c');
str.push_back('b');
str.push_back('a');

Solution sol;
std::sort(str.begin(), str.end(), sol);

for (size_t i=0; i<str.size(); i++) std::cout << str[i] << std::endl;
return 0;
}

That way the methods in your Solution object do not have to be static, and therefore they can access the mat member variable.

stringcomp: reference to non-static member function must be called

Member functions in C++ are special, they must be called from an instance of an object. The qsort function, being a holdover from C, doesn't know about objects and can't use it.

There are many ways to fix this. The first is to simply make stringcomp not a member function.

int stringcomp (const void *x, const void *y)

The second is to make it a static member function. Static functions don't need an object to be called against.

qsort (s, size - 1, sizeof *s, Par::stringcomp);

You should also consider using std::sort, it's the C++ way of doing sorting. You'll either need to implement operator< on your object, or provide a comparison function to the sort call. This comparison function is different from the one you use in C, and is a little beyond the scope of the question.



Related Topics



Leave a reply



Submit