Overloading []

I have the following overload.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
template <typename T>
class Set
{
protected:
	T**(items);
	int number;
public:

	//...

	T* Set::operator[] (int index)
	{
		return items[index];

	}

	//...
};


template <typename T>
class Matrix: public Set<Set<T>>
{
	//...
};


which has worked fine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

	Matrix<int> fbf(3,2);
	*(*fbf[0])[0] = 1;
	*(*fbf[0])[1] = 2;
	*(*fbf[1])[0] = 3;
	*(*fbf[1])[1] = 4;
	*(*fbf[2])[0] = 5;
	*(*fbf[2])[1] = 6;
	for(int j =0; j<fbf.getNumber(); ++j)
	{
		for(int i =0; i<fbf[0]->getNumber(); ++i)
			std::cout << *(*fbf[j])[i] << " ";
		std::cout << std::endl;
	}
	std::cout << std::endl;


1 2
3 4
5 6

Press any key to continue . . .



However I wish to use the [] operator to obtain and assign values alone, that is,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	Matrix<int> fbf(3,2);
	fbf[0][0] = 1;
	fbf[0][1] = 2;
	fbf[1][0] = 3;
	fbf[1][1] = 4;
	fbf[2][0] = 5;
	fbf[2][1] = 6;
	for(int j =0; j<fbf.getNumber(); ++j)
	{
		for(int i =0; i<fbf[0].getNumber(); ++i)
			std::cout << fbf[j][i] << " ";
		std::cout << std::endl;
	}
	std::cout << std::endl;


I am unsure of how to achieve this.

The following overload
1
2
3
4
5
	T Set::operator[] (int index)
	{
		return *items[index];

	}
errors with
Error 1 error C2106: '=' : left operand must be l-value

To fix your problem, you have to do two things.

First, the function you need:

 
T& Set::operator[]( int index );


BUT, the above declaration is ambiguous with your original one, so you will need to get rid of
your pointer version.

Next, I suggest also writing

 
T Set::operator[]( int index ) const;


which does the exact same thing as the first one I gave you except it is const and returns by
value rather than by reference.
I have changed it to,
1
2
3
4
	T& Set::operator[] (int index)
	{
		return *items[index];
	}

and now it work's perfectly.

I tired adding T Set::operator[]( int index ) const;
But I keep getting fatal error C1001: An internal error has occurred in the compiler.

1>(compiler file 'msc1.cpp', line 1393)
1> To work around this problem, try simplifying or changing the program near the locations listed above.
1>Please choose the Technical Support command on the Visual C++
1> Help menu, or open the Technical Support help file for more information


Not sure if this is a problem seeing as it currently works now. Thanks heaps.
What compiler are you using????? (Some msoft compiler I can see by the error message).

Without the const method you will not be able to call operator[] on a const Set. (Which might be fine
for what you are doing, but if you are creating a library that others will use, it will be annoying.)
I use VS8 (2005)
I'm creating a library for myself. Whether or not someone else will use it, I don't know :P
closed account (1yR4jE8b)
Although I'm not sure what that error message means, it does seem like the error your getting has nothing to do with your code because the compiler message says that it is an internal error. Have you ever tried the Gnu Compiler Collection? g++ is a very nice compiler and I find is more standard compliant than Microsoft's compiler. Try compiling your code with g++ and see if you are getting an error with that. If you are not comfortable compiling on the command line, you can also try the free Code::Blocks IDE, it is completely free and open source and comes set up with g++ as the default C++ compiler.

You can get g++ at www.mingw.org, and Code::Blocks at www.codeblocks.org

Visual C++ 2005 is known to not implement the standard C++ library correctly in a lot of key areas, if you can, try upgrading to the newest version. Visual C++ 2008 is a very good compiler and the Express version is free.

As for your overloads

Try changing:
 
T Set::operator[]( int index ) const;


to
 
const T& Set::operator[](int index) const;


Essentially, it does the same thing...but if you ever reuse your library to work with Sets of very large objects (personally, I've had to do collections of objects which take 200mB+ of memory) it is very expensive to return an object by value. If you return a const reference to the object, you do not return of copy of the object but a reference to it but because the reference is const you cannot change the data at that reference. That way you can save yourself a lot of copying time for large objects but still achieve the same const correctness. I'll concede that returning a reference to a primitive value like an int, is probably more expensive than just copying the value...but you are writing a template and the purpose of a template is to be portable and reusable for any data type or class. So I will argue with anyone and everyone that dissagrees that this is the "correct" way to overload that operator.

Thanks, loving C::B btw.
Topic archived. No new replies allowed.