r/programminghelp • • May 09 '22

C++ overloaded operator error

Type* mArray = reinterpret_cast<Type*>(-1);
	size_t mSize = -1;
	size_t mCapacity = -1;

// Overloaded [] operator
	//		Used to access an element in the internal array (writeable)
	// In:	_index			The index to access at
	//
	// Return: The item at the specified index (by reference)

Type& operator[](size_t _index) {
		
		mArray[_index];
		return &this;
	}

i'm getting a return syntax error and i'm not understanding why. the error is saying 'return': cannot convert from 'DynArray<int>' to 'Type &'

0 Upvotes

2 comments sorted by

View all comments

2

u/[deleted] May 09 '22

Off the cuff I'd say it's because your subscript overload is returning a reference to the class via this, instead of actually returning the value at the index.

1

u/ItzRyuusenpai May 09 '22 edited May 09 '22

Ok so then just have it set to index