#### Archived

This topic is now archived and is closed to further replies.

This topic is 5489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm having some problems with a class I'm writing. I'm writing a templated array class and I'd like to overload the [ ] operator. Here's how I tried to do this:
T* operator[] ( int pIndex )
{
return &m_array[pIndex];
}

This doesn't cause any errors, however, it doesn't work. I use almost identical code on a function "ReturnElement()" and it works:
T* Array<T>::ReturnElement(int index)
{
return &m_array[index];	//Return the desired element

};

The ReturnElement function works (not compleatly as desired, I don't want to use indirection all the time but I think that I can just return the address, this can be changed later though):
list = new array<int>(10);
*list->ReturnElement(2) = 21;
cout << *list->ReturnElement(2) << endl;

This works fine but when I try it with the [ ] operator I get illegal indirection errors and when assigning into a variable it gives me the following error: c:\Documents and Settings\David.000\My Documents\Visual Studio Projects\LockerProblems\main.cpp(17): error C2440: 'initializing' : cannot convert from 'Array' to 'int' with [ T=int ] No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Anyone know what I'm doing wrong? Thanks in advance! "Everything that can be invented has been invented." -Charles H. Duell, Commissioner, U.S. Office of Patents, 1899. [edited by - perfectly_dark on November 7, 2003 10:17:53 PM] [edited by - perfectly_dark on November 7, 2003 10:18:31 PM]

##### Share on other sites
Maybe use Array< T>::operator[], I'm not sure, haven't used C++ in a while.

[edited by - smart_idiot on November 7, 2003 10:22:17 PM]

##### Share on other sites
well, I defined the operator in the class declaration. The ReturnElement() function was defined outside the class so I had to use Array::ReturnElement(). It shouldn''t make a difference if I remember correctly but might as well give it a try.

##### Share on other sites
Just as I thought, no dice. Thanks for the idea though, any more?

##### Share on other sites
T& operator[] ( int pIndex ){	return m_array[pIndex];}   T& Array<T>::ReturnElement(int index){	return m_array[index];	//Return the desired element};list = new Array<int>(10);list->ReturnElement(2) = 21;cout << list->ReturnElement(2) << endl;

[edited by - smart_idiot on November 7, 2003 10:40:55 PM]

##### Share on other sites
Thanks, but it still doesn't work. The [ ] operator still gives me the error about Array datatype conversion. When I use ReturnElement(2) = 10, the actual value in the array doesn't change. I tried this by initializing the values to 0, and when I assign a new number using the ReturnElement() method, it doesn't change.

[edited by - perfectly_dark on November 7, 2003 10:48:02 PM]

[edited by - perfectly_dark on November 7, 2003 10:49:58 PM]

##### Share on other sites
I'll expand on SmartIdiot's code.

template<typename T>T& Array<T>::operator[] ( int pIndex ){	return m_array[pIndex];}template<typename T>const T& Array<T>::operator[] ( int pIndex ) const{  return m_array[pIndex]}

Unless your member functions are defined in the body of the class, which makes both template and Array:: unnecessary (and incorrect).

This is also true for your ReturnElement function.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on November 7, 2003 12:45:56 AM]

##### Share on other sites
Yes, however, most of my code was outside the body of the class so I did use those.

##### Share on other sites
quote:
Original post by perfectly_dark
Yes, however, most of my code was outside the body of the class so I did use those.

By the way, you could have a look at this class

Boost is your friend. Use it. Cherish it. Scream in frustration when you try to build it (the few bits that need to be compiled).

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

##### Share on other sites
My code was out of context, I just didn''t feel like making 3 source windows, and I didn''t know where those functions were. This is why it''s best to post code that actually compiles, so people don''t have to guess.

Anywho, look into std::vector. It''s probably what you''re trying to emulate.

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 14
• 14
• 9
• ### Forum Statistics

• Total Topics
633376
• Total Posts
3011558
• ### Who's Online (See full list)

There are no registered users currently online

×