What's wrong Overloading []

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SinusX

    What's wrong Overloading []

    Hello
    My gcc copiler write this
    TypEle& Tablica<TypEle> ::operator[](unsigned int) [with TypEle = int]
    ' and `
    TypEle Tablica<TypEle> ::operator[](unsigned int) [with TypEle = int]'
    cannot be overloaded

    where TypEle is tempalte type, Tablica is table object.

    TypEle Tablica<TypEle> ::operator[](unsigned int) [with TypEle = int]
    I use for Tablica<int> tab1; int X;
    X = tab1[23] and this work great but when I added
    TypEle& Tablica<TypEle> ::operator[](unsigned int) [with TypEle = int]
    for tab[1] = X it doen't work. With second operator i can't compile. How
    should i corect this ??


  • Victor Bazarov

    #2
    Re: What's wrong Overloading []

    SinusX wrote:[color=blue]
    > My gcc copiler write this
    > TypEle& Tablica<TypEle> ::operator[](unsigned int) [with TypEle = int]
    > ' and `
    > TypEle Tablica<TypEle> ::operator[](unsigned int) [with TypEle = int]'
    > cannot be overloaded
    >
    > where TypEle is tempalte type, Tablica is table object.
    >
    > TypEle Tablica<TypEle> ::operator[](unsigned int) [with TypEle = int]
    > I use for Tablica<int> tab1; int X;
    > X = tab1[23] and this work great but when I added
    > TypEle& Tablica<TypEle> ::operator[](unsigned int) [with TypEle = int]
    > for tab[1] = X it doen't work. With second operator i can't compile. How
    > should i corect this ??[/color]

    The compiler complains because the difference between the two functions is
    only in the return value type. You probably mean to make the second one
    'const':

    TypEle Tablica<TypEle> ::operator[](unsigned int) const;

    while leaving the first one as is:

    TypEle& Tablica<TypEle> ::operator[](unsigned int);

    See the difference? The one that returns a reference should not be const,
    most likely.

    Victor

    Comment

    Working...