[try Beta version]
Not logged in

 
invalid use of non-static member function

Jun 25, 2020 at 1:01pm
Hi Community,
i need sort after inser elements, but after the shell show this error:

1
2
3
4
5
error: invalid use of non-static member function ‘int compare(const void*, const void*)’
     qsort(this->nodes, this->vertices, sizeof(int), compare)"here";

declared here
 int "declare here" compare (const void * a, const void * b)


1
2
3
4
5
6
7
8
class GenericNode
{
private:
    int id;
    int weight;
    GenericNode* Prev;
    list<pair<GenericNode*, int>> *adj;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Graph
{
private:
    int arches;
    int vertices;
    vector<GenericNode*> *nodes;
public:
    Graph()
    {
        this->arches = 0;
        this->vertices = 0;
        this->nodes = new vector<GenericNode*>;
    }
    int Graph::compare (const void * a, const void * b)
    {
        return ( *(int*)a - *(int*)b );
    }
    void setNode(GenericNode* node)
    {
        this->nodes->push_back(node);
    }
    void insert()
    {
        this->vertices=10;
        for(int i=0; i < this->vertices; i++){
            this->setNode(new GenericNode(i+1));
        }
        qsort(this->nodes, this->vertices, sizeof(int), compare);
    }
}


1
2
3
4
5
int main()
{
    Graph Q;
    Q.insert();   
}
Jun 25, 2020 at 1:31pm
1
2
3
4
5
//MUST be static:
static int Graph::compare (const void * a, const void * b)
{
    return ( *(int*)a - *(int*)b );
}

You can also simply pass a lambda to qsort:
 
qsort(this->nodes, this->vertices, sizeof(int), &[](auto a, auto b){ return ( *(int*)a - *(int*)b ); });
Jun 25, 2020 at 1:38pm
i try but:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
error: taking address of temporary [-fpermissive]
 rtices, sizeof(int), &[](auto a, auto b){ return ( *(int*)a - *(int*)b ); });
                                                                           ^

Graph.cpp:103:53: error: cannot convert ‘Graph::insert()::<lambda(auto:1, auto:2)>*’ to ‘__compar_fn_t’ {aka ‘int (*)(const void*, const void*)’}
     qsort(this->nodes, this->vertices, sizeof(int), &[](auto a, auto b){ return ( *(int*)a - *(int*)b ); });
In file included from /usr/include/c++/8/cstdlib:75,
                 from /usr/include/c++/8/ext/string_conversions.h:41,
                 from /usr/include/c++/8/bits/basic_string.h:6400,
                 from /usr/include/c++/8/string:52,
                 from Graph.hpp:5,
                 from Graph.cpp:4:
/usr/include/stdlib.h:828:20: note:   initializing argument 4 of ‘void qsort(void*, size_t, size_t, __compar_fn_t)’
      __compar_fn_t __compar) __nonnull ((1, 4));


and if try static near int:

1
2
error: cannot declare member function ‘static int Graph::compare(const void*, const void*)’ to have static linkage [-fpermissive]
 static int Graph::compare (const void * a, const void * b)
Last edited on Jun 25, 2020 at 1:39pm
Jun 25, 2020 at 1:58pm
I thought lambdas w/ nothing in the [] can be converted into function pointers.
What compiler are you using?
Jun 25, 2020 at 2:09pm
Have you considered using std::sort instead of qsort()?

Jun 25, 2020 at 2:32pm
g++ in linux

now try sort()

 
sort( this->nodes->begin(), this->nodes->begin()+this->vertices );


can you see if it is correct?
Last edited on Jun 25, 2020 at 2:37pm
Jun 25, 2020 at 3:01pm
I question a few things about your implementation, for example why is nodes a pointer to a std::vector? Wouldn't a "normal" non-pointer vector work?

1
2
3
4
5
6
7
8
9
10
11
12
class Graph
{
private:
    int arches;
    int vertices;
    vector<GenericNode*> nodes;
public:
    Graph() : arches(0), vertices(0);
    {
        // Blank body.
    }

For the std::sort you will probably want to overload the less than comparison operator in your GenericNode class.

The std::sort would then be something like:

std::sort(nodes.begin(), nodes.end());

And note that the this-> is usually not required unless you have a "name clash" with a parameter.

Jun 25, 2020 at 3:06pm
ok i use it, i think it works, but it didn't solve the problem i had with the graph, too bad, but thanks for the help.
Jun 25, 2020 at 3:15pm
but it didn't solve the problem i had with the graph,

What problem is that?

You haven't provided enough code for anyone to really see what you're trying to do so you would need to ask specific questions and provide the actual code, not just random snippets that won't even come close to compiling.

Jun 25, 2020 at 3:19pm
I thought my problem was about ordering the graph, but that's not it.
I have a problem on building MST with Prim sometimes it gives me the exact result and other times wrong, then sometimes if I order the file before inserting it it gives me the exact result, other times it gives me the wrong result anyway, I've been going crazy for days I can't to understand the reason.
but I can't post all the code because it's a lot
Topic archived. No new replies allowed.