Jump to content

Overload the [] operator with a string.

- - - - -

  • Please log in to reply
2 replies to this topic

#1
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
Hello,
I am writing a C++ class over the C PyObject so it can be handled more easily, for example when working with arrays, calling PySequence_GetItem a hundred times gets really annoying :D
I successfully managed to make something like "data[0][12].c_str()" to return the proper value.

Now, when I try to overload the [] operator with a const char*(to make dicts with string keys work, I have alot of them), I get a somewhat logical complaint from gcc. It says the overload is ambiguous; I guess gcc doesn't know whether the number I give as an argument is a char* or a size_t:
error: ambiguous overload for 'operator[]' in 'temp[0]'
.\PyVariable.h|13|note: candidates are: PyVariable PyVariable::operator[](size_t)
.\PyVariable.h|14|note:                 PyVariable PyVariable::operator[](const char*)


So... how do I fix this :P ?

Thanks for you help.

#2
thechef

thechef

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Hmm, I can't seem to reproduce your error. I am using CodeBlocks, and it compiled fine. Could you maybe use something other than size_t as the parameter type? Perhaps something more obviously different than char *? I am not too familiar with size_t, so I can't really say for sure what's going on.
I don't document code. If it was hard to write, it should be hard to read ;)

#3
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
Strange that you don't have the error.
I replaced size_t, with an int it works, with an unsigned int(same type as any pointer) it doesn't.
So, I guess I'll have to keep arrays smaller than 2147483648 elements... shouldn't be a problem ;)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users