Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Operator [] Overloading

Operator [] Overloading

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelp
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mikey_H
    wrote on last edited by
    #1

    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.

    CPalliniC S C 3 Replies Last reply
    0
    • M Mikey_H

      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.

      CPalliniC Online
      CPalliniC Online
      CPallini
      wrote on last edited by
      #2

      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# (or Java) 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]

      In testa che avete, signor di Ceprano?

      M 1 Reply Last reply
      0
      • M Mikey_H

        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.

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        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 of array[x] is Array<AssocArray<int>>& - and that has an operator[] that takes an integer index, not a string index. So, either declare array as Array<AssocArray<int>> array(); or use (*array)[x][str]

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        M 1 Reply Last reply
        0
        • S Stuart Dootson

          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 of array[x] is Array<AssocArray<int>>& - and that has an operator[] that takes an integer index, not a string index. So, either declare array as Array<AssocArray<int>> array(); or use (*array)[x][str]

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          M Offline
          M Offline
          Mikey_H
          wrote on last edited by
          #4

          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

          S 1 Reply Last reply
          0
          • CPalliniC CPallini

            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# (or Java) 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]

            M Offline
            M Offline
            Mikey_H
            wrote on last edited by
            #5

            Few typos there, C# :laugh: I have made a reply to the other response.

            1 Reply Last reply
            0
            • M Mikey_H

              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.

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              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++

              M 1 Reply Last reply
              0
              • M Mikey_H

                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

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                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 be pa->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

                M 1 Reply Last reply
                0
                • S Stuart Dootson

                  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 be pa->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

                  M Offline
                  M Offline
                  Mikey_H
                  wrote on last edited by
                  #8

                  Ok, I got it working, I had it right by the last response, the next error that came up was somewhere else unrelated and I was too tired to notice. Thank you very much for your help.

                  1 Reply Last reply
                  0
                  • C Cedric Moonen

                    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++

                    M Offline
                    M Offline
                    Mikey_H
                    wrote on last edited by
                    #9

                    Just an attempt to learn a little more.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups