Compile error on priority_queue.

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Compile error on priority_queue.

Post by ImLazy »

Code: Select all

#include <queue>

using namespace std;

struct MyLess: public less<int> {
    const int* a;

    MyLess(const int* _a) {
        a = _a;
    }

    bool operator () (int i, int j) const {
        return a[i] > a[j];
    }
};

int main() {
    int a[100];
    priority_queue<int> que( MyLess(a) );
    que.push(1);
    return 0;
}
But I get this compilation error from g++ 4.1.2:

Code: Select all

test-pre.cpp:20: error: request for member 'push' in 'que', which is of non-class type 'std::priority_queue<int, std::vector<int, std::allocator<int> >, std::less<int> > ()(MyLess)'
I know this code is not good, because when changing the array a, the order in priority_queue may not change, although it should. I know it should be written in other way, but I just wonder why this code get compilation error.
I stay home. Don't call me out.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

You should specify MyLess in template parameters like this:
MyLess comparer(a);
priority_queue<int, vector<int>, MyLess> que(comparer);

ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Post by ImLazy »

:-? You can try, I change the line

Code: Select all

priority_queue<int> que( MyLess(a) ); 
to

Code: Select all

priority_queue<int, vector<int>, MyLess> que( MyLess(a) );
still the same compilation error.
I stay home. Don't call me out.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

If you write it exactly as I wrote, there wouldn't be an error.
But I'm not sure why it is so.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

It looks like the compiler thinks that "priority_queue<int, vector<int>, MyLess> que(MyLess(a));" is a function prototype, instead of a variable.

ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Post by ImLazy »

mf wrote:It looks like the compiler thinks that "priority_queue<int, vector<int>, MyLess> que(MyLess(a));" is a function prototype, instead of a variable.
Maybe just as you said, we can see the compiler outputs "which is of non-class type". So I change the line to:

Code: Select all

priority_queue<int, vector<int>, MyLess> que = priority_queue<int, vector<int>, MyLess>( MyLess(a) );
just to tell the compiler que is an object, and this code works well as I want.
Also, your code works well. I missed the first line of your code just now, and thought you just told me to add two template parameters, sorry. :lol:
I stay home. Don't call me out.

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

mf wrote:It looks like the compiler thinks that "priority_queue<int, vector<int>, MyLess> que(MyLess(a));" is a function prototype, instead of a variable.
Yes, this declaration is equivalent to

Code: Select all

priority_queue<int, vector<int>, MyLess> que(MyLess a);
which is a declaration of function que that takes a single parameter of MyLess type called a and returns a priority_queue, the brackets around a don't make any difference.

Another way to make this code work would be to use

Code: Select all

priority_queue<int> que(static_cast<MyLess>(a));
(static_cast is in this context equivalent to calling a constructor)
For millions of years, mankind lived just like the animals. Then something happened which unleashed the power of our imagination. We learned to talk and we learned to listen...

Post Reply

Return to “C++”