Operator [] Overloading
-
I have made a few wrapper classes for arrays to include dynamic resizing, string indexing etc. The classes worked fine when used on their own in an app. Now i have attempted to make a Hashtable class which uses an Array object of AssocArray objects. When I attempt to access data by using code like ' array[x][str] = y; ' I get the following error... error C2679: binary '[' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
#include <string>
using std::string;int x = 0;
int y = 1;string = "Foo";
array = new Array<AssocArray<int>>();
// Error C2679
array[x][str] = y;Function for overloaded operators is very similar for both Array, and AssocArray. I have removed all code inside function as it produced the same error when commented out.
template <class TType>
TType& AssocArray<TType>::operator [](string s)
{
return new TType;
}It is my understanding that the line ' array[x][str] = y; ' would be computed like this...
AssocArray ax = array[x];
ax[str] = y;Am I correct about this? I'm not sure what is going on here, and assume this is all the relevant code, If you would like to see any more just ask.
-
I have made a few wrapper classes for arrays to include dynamic resizing, string indexing etc. The classes worked fine when used on their own in an app. Now i have attempted to make a Hashtable class which uses an Array object of AssocArray objects. When I attempt to access data by using code like ' array[x][str] = y; ' I get the following error... error C2679: binary '[' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
#include <string>
using std::string;int x = 0;
int y = 1;string = "Foo";
array = new Array<AssocArray<int>>();
// Error C2679
array[x][str] = y;Function for overloaded operators is very similar for both Array, and AssocArray. I have removed all code inside function as it produced the same error when commented out.
template <class TType>
TType& AssocArray<TType>::operator [](string s)
{
return new TType;
}It is my understanding that the line ' array[x][str] = y; ' would be computed like this...
AssocArray ax = array[x];
ax[str] = y;Am I correct about this? I'm not sure what is going on here, and assume this is all the relevant code, If you would like to see any more just ask.
Mikey_H wrote:
#include using std::string; int x = 0; int y = 1; string = "Foo"; array = new Array<AssocArray<int>>(); // Error C2679 array[x][str] = y;
does it stands for:
int x = 0;
int y = 1;string str = "Foo";
Array<AssocArray<int>> * array;array = new Array<AssocArray<int>>();
(*array)[x][str] = y;?
Mikey_H wrote:
template TType& AssocArray<TType>::operator [](string s) { return new TType; }
does it stands for
TType& AssocArray<TType>::operator [](string s)
{
return *(new TType);
}? Have you
C#
(orJava
) roots? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I have made a few wrapper classes for arrays to include dynamic resizing, string indexing etc. The classes worked fine when used on their own in an app. Now i have attempted to make a Hashtable class which uses an Array object of AssocArray objects. When I attempt to access data by using code like ' array[x][str] = y; ' I get the following error... error C2679: binary '[' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
#include <string>
using std::string;int x = 0;
int y = 1;string = "Foo";
array = new Array<AssocArray<int>>();
// Error C2679
array[x][str] = y;Function for overloaded operators is very similar for both Array, and AssocArray. I have removed all code inside function as it produced the same error when commented out.
template <class TType>
TType& AssocArray<TType>::operator [](string s)
{
return new TType;
}It is my understanding that the line ' array[x][str] = y; ' would be computed like this...
AssocArray ax = array[x];
ax[str] = y;Am I correct about this? I'm not sure what is going on here, and assume this is all the relevant code, If you would like to see any more just ask.
Mikey_H wrote:
template TType& AssocArray::operator [](string s) { return new TType; }
That doesn't look quite right - typo somewhere?
Mikey_H wrote:
array = new Array<AssocArray<int>>(); // Error C2679 array[x][str] = y;
Similarly here - you can't use operators directly with pointers - but now I understand the error message. The type of array is
Array<AssocArray<int>>*
. So, the type ofarray[x]
isArray<AssocArray<int>>&
- and that has anoperator[]
that takes an integer index, not a string index. So, either declare array asArray<AssocArray<int>> array();
or use(*array)[x][str]
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Mikey_H wrote:
template TType& AssocArray::operator [](string s) { return new TType; }
That doesn't look quite right - typo somewhere?
Mikey_H wrote:
array = new Array<AssocArray<int>>(); // Error C2679 array[x][str] = y;
Similarly here - you can't use operators directly with pointers - but now I understand the error message. The type of array is
Array<AssocArray<int>>*
. So, the type ofarray[x]
isArray<AssocArray<int>>&
- and that has anoperator[]
that takes an integer index, not a string index. So, either declare array asArray<AssocArray<int>> array();
or use(*array)[x][str]
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Stuart Dootson wrote:
you can't use operators directly with pointers
Ok, I did not think about that. So (*varPointer) will derefernce this? (Is this only for user defined types? A pointer to a standard C array does not have to be derferenced does it?) That first issue is solved with the corrections, another small one exists tho, probably a similar issue.
(*array)[x][str] = y;
will give the error.... error C2440: 'return' : cannot convert from 'int' to 'AssocArray<TType> &' Do I need to dereference the AssocArray somehow too? Thank you for your responses :)
modified on Tuesday, May 26, 2009 5:27 AM
-
Mikey_H wrote:
#include using std::string; int x = 0; int y = 1; string = "Foo"; array = new Array<AssocArray<int>>(); // Error C2679 array[x][str] = y;
does it stands for:
int x = 0;
int y = 1;string str = "Foo";
Array<AssocArray<int>> * array;array = new Array<AssocArray<int>>();
(*array)[x][str] = y;?
Mikey_H wrote:
template TType& AssocArray<TType>::operator [](string s) { return new TType; }
does it stands for
TType& AssocArray<TType>::operator [](string s)
{
return *(new TType);
}? Have you
C#
(orJava
) roots? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I have made a few wrapper classes for arrays to include dynamic resizing, string indexing etc. The classes worked fine when used on their own in an app. Now i have attempted to make a Hashtable class which uses an Array object of AssocArray objects. When I attempt to access data by using code like ' array[x][str] = y; ' I get the following error... error C2679: binary '[' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
#include <string>
using std::string;int x = 0;
int y = 1;string = "Foo";
array = new Array<AssocArray<int>>();
// Error C2679
array[x][str] = y;Function for overloaded operators is very similar for both Array, and AssocArray. I have removed all code inside function as it produced the same error when commented out.
template <class TType>
TType& AssocArray<TType>::operator [](string s)
{
return new TType;
}It is my understanding that the line ' array[x][str] = y; ' would be computed like this...
AssocArray ax = array[x];
ax[str] = y;Am I correct about this? I'm not sure what is going on here, and assume this is all the relevant code, If you would like to see any more just ask.
Is there a specific reason why you don't want to use the containers from the STL (std::map, std::vector, ...) ? All these classes already provide support for what you are doing, so why do you want to rewrite that ?
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
Stuart Dootson wrote:
you can't use operators directly with pointers
Ok, I did not think about that. So (*varPointer) will derefernce this? (Is this only for user defined types? A pointer to a standard C array does not have to be derferenced does it?) That first issue is solved with the corrections, another small one exists tho, probably a similar issue.
(*array)[x][str] = y;
will give the error.... error C2440: 'return' : cannot convert from 'int' to 'AssocArray<TType> &' Do I need to dereference the AssocArray somehow too? Thank you for your responses :)
modified on Tuesday, May 26, 2009 5:27 AM
Mikey_H wrote:
(Is this only for user defined types? A pointer to a standard C array does not have to be derferenced does it?)
Quite right - pointers are special cases :-) Think about it - you're applying an operator defined on a class. The operator syntax
a[b]
is equivalent to a.operator[](b). So, with a pointer to the object, you can see that the method call syntax would bepa->operator[](b)
, or(*pa).operator[](b)
.Mikey_H wrote:
That first issue is solved with the corrections, another small one exists tho, probably a similar issue. (*array)[x][str] = y; will give the error.... error C2440: 'return' : cannot convert from 'int' to 'AssocArray &' Do I need to dereference the AssocArray somehow too?
Here's some sample code I wrote that has the
operator[]
layout you want:#include <string>
#include <iostream>template<class Element>
class Array
{
public:
Element& operator[](int index) { return e; }
private:
Element e;
};template<class Element>
class AssocArray
{
public:
Element& operator[](std::string const& s) { return e; }
private:
Element e;
};int main()
{
Array< std::string > as;
AssocArray< std::string > aas;
Array< AssocArray< std::string > > aaas;as[2];
aas["Hello"];aaas[2]["Hello"] = "Test";
std::cout << aaas[2]["Hello"] << std::endl;
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Mikey_H wrote:
(Is this only for user defined types? A pointer to a standard C array does not have to be derferenced does it?)
Quite right - pointers are special cases :-) Think about it - you're applying an operator defined on a class. The operator syntax
a[b]
is equivalent to a.operator[](b). So, with a pointer to the object, you can see that the method call syntax would bepa->operator[](b)
, or(*pa).operator[](b)
.Mikey_H wrote:
That first issue is solved with the corrections, another small one exists tho, probably a similar issue. (*array)[x][str] = y; will give the error.... error C2440: 'return' : cannot convert from 'int' to 'AssocArray &' Do I need to dereference the AssocArray somehow too?
Here's some sample code I wrote that has the
operator[]
layout you want:#include <string>
#include <iostream>template<class Element>
class Array
{
public:
Element& operator[](int index) { return e; }
private:
Element e;
};template<class Element>
class AssocArray
{
public:
Element& operator[](std::string const& s) { return e; }
private:
Element e;
};int main()
{
Array< std::string > as;
AssocArray< std::string > aas;
Array< AssocArray< std::string > > aaas;as[2];
aas["Hello"];aaas[2]["Hello"] = "Test";
std::cout << aaas[2]["Hello"] << std::endl;
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Is there a specific reason why you don't want to use the containers from the STL (std::map, std::vector, ...) ? All these classes already provide support for what you are doing, so why do you want to rewrite that ?
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++