Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sammich

macrumors 601
Original poster
Sep 26, 2006
4,305
268
Sarcasmville.
This is part of an assignment for uni, where we need to construct a very simple query engine. The entire project compiles bar these two errors below, and I've asked my lecturer for help, he gave it a good look and stopped just short of coding the solution for me (yes, he's a good lecturer).

My problem, he says, is that the compiler thinks that my vector list, 'aggregate', is a const object. Which it isn't. And I can remedy this my using 'const_cast' to convert it. I've done just that, and that leads me to the first error, where I tried to dereference the pointer I made to convert it to a non-const.

What I tried to do:
vector aggregate // apparently const
aggregate_pointer = const_cast (aggregate)
aggregate = dereference of aggregate_pointer

The second error is what I tried to fix with the first. So if anyone can tell me if a const_cast really is the solution to this, it would be great.

If you need the rest of the code, just say so and I'll post the zip of the code.

Thanks in advance.

Code:
class Disjunction : public Node {
	std::vector<Node *> list;
	std::vector<Hits> aggregate;
public:
	Disjunction(std::vector<Node *> nodeList) {
		list = nodeList;
	}
	
	virtual Hits operator()(const Context *context) const {
		Hits result;
		
		std::vector<Hits> &aggregate_p = const_cast<std::vector<Hits> &>(aggregate);
		
		[COLOR="Red"]// ** FIRST ERROR OCCURS HERE **
		aggregate = *aggregate_p;[/COLOR]
		
		// Base case: a single conjuction, pass on evaluation to child
		if (list.size() == 1) {
			Node * anode = *list.begin();
			return (*anode)(context);
		}
		
		std::vector<Node *>::const_iterator node_p;
		for (node_p = list.begin(); node_p != list.end(); ++node_p) {
			Node * anode = *node_p;

			[COLOR="Red"]// ** SECOND ERROR OCCURS HERE **
			aggregate.push_back( (*anode)(context) );[/COLOR]
		}
Errors:
Code:
subNode.cc:23: error: no match for ‘operator*’ in ‘*aggregate_p’
subNode.cc:35: error: passing ‘const std::vector<Hits, std::allocator<Hits> >’ as ‘this’ argument of ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Hits, _Alloc = std::allocator<Hits>]’ discards qualifiers
 
Hi

You've declared operator() as const but then the rest of your code goes on to modify aggregate. So do you really want operator() to be const? If so then you might want to declare aggregate as mutable and get rid of

Code:
st std::vector<Hits> & aggregate_p = const_cast<std::vector<Hits> &>(aggregate);

(that line looks like it's there because you're trying to get round the errors arising from the declaration of operator() as const).

ß e n
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.