hi everyone,
i'm having trouble initializing a private int in a class and can't figure out what's wrong. it's especially confusing me because i do nothing different with my int called size from with my int called HeapSize, but size won't compile. the error i get is: "error: invalid use of member (did you forget the '&'?).

from my .h file:

#ifndef _pqheap_h
#define _pqheap_h
#include "genlib.h"

class PQueue {
public:
	PQueue();
	~PQueue();
private:
	int HeapSize;
	int *heap;
	int size;
	void swap(int p1, int p2);
};

#endif

from my .cpp file:

#include "genlib.h"
#include <iostream>
#include "pqheap.h"
#include "simpio.h"
PQueue::PQueue()
{
	size = 0;  // error occurs here
	HeapSize = 10;  // but not here
    heap = new int[HeapSize];
}

Recommended Answers

All 5 Replies

Works fine for me in Visual C++ 2005.

Ok there is no way for us to know the problem but you most likely have a macro #define size .... somewhere in the mess of headers.

Have you stripped this down to the minimum- does it still work.

If you put a line #undef size just above the PQueue::PQueue line does it still go wrong??

The code you have posted without the #include "junk.h" works fine.

Finally, if it is a #define problem, it shows one of the subtle ways in which #defines are evil.
If you had used a const int size(50); then the code would have worked perfectly, since the scoping rules prevent a clash.

Hmm. Thanks both for the replies. I'm using XCode, if that makes a difference. I don't have any other headers aside from the ones I included in the post, and no #define 's. What really strikes me as odd is that it only gives the error for the int size and not HeapSize. Thanks again for taking a look.

Yea, I'd guess 'size' is either a "reserved word" (not technically?) - just try to rename the variable and see if that works.

you're right! wow, how silly of me. thanks so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.