operator() overload

I've got 2 classes, Store and Transaction and I would like to create a priority queue of objects Transaction as a variable of Store class. I actually already asked similar question couple days ago and you were able to help, but those operators seems to be my Nemesis.

My store.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef __STORE_H_INCLUDED__   
#define __STORE_H_INCLUDED__ 

#include <queue>

using namespace std;

#include "Transaction.h"

struct TransactionCompare;
class Transaction;

class Store {

priority_queue<Transaction*, vector<Transaction*>, TransactionCompare> store;

           ...

};

#endif


And my Transaction.h, Transaction.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef __TRANSACTION_H_INCLUDED__   
#define __TRANSACTION_H_INCLUDED__ 

#include <iostream>

using namespace std;

class Calendar;
class Store;
class Transaction;

struct TransactionCompare {
	bool operator()(const Transaction &t1, const Transaction &t2) const;
};

class Transaction {

...

};

#endif 


1
2
3
4
5
6
7

...

bool TransactionCompare::operator()(const Transaction &t1, const Transaction &t2) const {
	return t1.getPriority() < t2.getPriority();
}


The error im getting with this set up is

1
2
error C2664: 'bool TransactionCompare::operator ()(const Transaction &,const Transaction &) 
const' : cannot convert parameter 1 from 'Transaction *' to 'const Transaction &'


Would be happy for any help,

Thanks!
Last edited on
I noticed a possible solution to your problem. I noticed that the priority_queue has a pointer to a transaction, and the operator()(...) takes in a reference. Did you make sure to convert the pointer to a reference?

Of course, this is just guessing from your code, and I don't have any personal experience with std::queue


EDIT: Nevermind. After reading the next one, I've got a feeling my hypothesis was definitely wrong.
Last edited on
If you are trying to overload an operator in your class, the operator parameters can only contain zero or one other object. This is because the left hand side of the operator is substituted as the first argument to the operator and the other object on the left hand side of the operator is used as the second argument.

To fix any errors you have, you can make all the operators in your class friends to the class, that way they can take 2 parameters

friend bool operator()(const Transaction *t1, const Transaction *t2) const;
Last edited on
Could you please show me exactly how to implement the operator then? I've been playing around with it for a while, but with no success.
@Smac89: last time I checked, operator() couldn't be a global operator, thus it can't be a friend.
I managed to get rit of the errors by implementing it like this:

transaction header
 
bool operator < (const Transaction &) const;


transaction cpp file
1
2
3
4
5
...

bool Transaction::operator <(const Transaction & b) const{
	return this->getPriority() > b.getPriority();
}


The way I declare the queue in store.h is now

 
priority_queue<Transaction*> store;


Well, it does not give me any errors, but the queue seems to remain unsorted. Like the operator has been completely ignored.

Any ideas?
If you have a container of pointers, your comparison function needs to compare.. pointers. Your Transaction::operator< is not used at all.

1
2
3
4
bool operator<(const Transaction* a, const Transaction* b)
{
    return a->getPriority() > b->getPriority() ;
}
Topic archived. No new replies allowed.