error: No operator matches these operands

Hey, I'm having an issue with this array within my overloaded << opperator. It keeps saying:
No operator "[]" matches these operands.


The set function is working properly

1
2
3
4
5
6
7
8
9
10
11
ostream& operator<< (ostream& out, const BitArray& ba)//OR I can simply do a int to binary conversion
{
	for(int j = 0; j < 5; j++)//Arraysize wont work?
		for(int i = 0; i < BITS_PER_BYTE; i++)
		{
			if(ba[j].Set(i)==1)// <- Issue here
				out << "1";
			else
				out << "0";
		} 
}
Please show how ba is defined, and how you are calling this function. Also show your Set() function definition and implementation.

Last edited on
Set() Function:
1
2
3
4
5
6
7
8
9
10
11
12
13
void BitArray::Set(unsigned int index)
{
	int mask = 1;
	double byte = (double)index/BITS_PER_BYTE;//
	int position = index%BITS_PER_BYTE;
	mask = mask << index;

	if(debug == true)
		cout << endl << "Index: " << index << " mask: " << mask << " byte: " << byte << " position: " << position << endl;
	char val = (barray[(int)byte] | mask);
	if(debug ==true)
		cout << val;
}


Currently, the function isn't being called. the "ba[j]" part is underlined in red.
It's also kicking this error out: error C2228: left of '.Set' must have class/struct/union
And the class:
1
2
3
4
5
6
7
8
9
10
11
12
13
class BitArray
{
	friend ostream& operator<< (ostream& out, const BitArray& ba);
public:
	BitArray(unsigned int a);
	void Set(unsigned int index);
	//Unset(unsigned int index);
		
private:
	unsigned char* barray;
	int arraySize;
	bool debug;
};
How are you actually calling this function? It doesn't appear that you are passing any kind of array to the function. It looks like you're passing a single instance of your class not an array.

Also Set() is not returning anything so you can't do the comparison in the if statement.

Why are you calling a function with the name "Set" in an output function? Should you maybe be using a different function?


The reason why it looks like I'm passing a single instance is because I'm supposed to be passing it by reference.

And I see what your saying for the Set() thing, that makes sense.

After playing around, I notice I'm having errors calling any member functions aside from the constructor.
You need to define the [] operator for your class. Right now, ba[j] doesn't mean anything.

Unless you're actually passing an array of BitArray objects, in which case this

ostream& operator<< (ostream& out, const BitArray& ba)

should be

ostream& operator<< (ostream& out, const BitArray* ba)
Last edited on
fg109: When I make that change, it tells me
type qualifier is not compatable with this memeber function
All I'm saying is that you can't use ba[j]. By default, the [] operator is only used for arrays. So, there are only two ways that ba[j] would be valid code. Either ba is an array of BitArray objects, or else you defined the [] operator for your BitArray class.

Now I have no idea how you're using the function, or what your class is supposed to do, but I'm going to guess that you don't want to pass an array to that function. In which case you shouldn't change

1
2
3
4
5
ostream& operator<< (ostream& out, const BitArray& ba)[/cod]e

to

[code]ostream& operator<< (ostream& out, const BitArray* ba)


What you should do is figure out what you meant by ba[j] and replace that with code that actually means something.

As for the other errors you got:

error C2228: left of '.Set' must have class/struct/union

That would be because ba[j] doesn't mean anything, it's totally undefined. It's not a BitArray object, so it doesn't have a Set function.

type qualifier is not compatable with this memeber function

That would be because you changed the function definition without changing the function protocol in your class definition. Which you can forget about since, as I said, you probably weren't trying to pass an array.
Topic archived. No new replies allowed.