Operator[] error

This is a discussion on Operator[] error within the C++ Programming forums, part of the General Programming Boards category; I've just ran into a problem with this: Code: #include "stdafx.h" using namespace std; class HashTable { int data[100]; public: ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Operator[] error

    I've just ran into a problem with this:

    Code:
    #include "stdafx.h"
    
    using namespace std;
    
    class HashTable
    {
        int data[100];
    
    public:
        HashTable();
        ~HashTable();
    
        int operator[](char *rhs);
    };
    
    HashTable::HashTable()
    {
        for (int i = 0; i < 100; i++)
            data[i] = 0;
    }
    
    HashTable::~HashTable()
    {
    
    }
    
    // operator[]
    int HashTable::operator[](char *rhs)
    {
        int hash = 0;
        int i = 0;
    
        // construct a hash from the string
        for (; *(rhs + i) != 0; i++)
            hash += static_cast<int>(*(rhs + i));
    
        cout << "hash: " << hash << endl;
        return 0;
    }
    
    
    int main(int argc, char* argv[])
    {
        HashTable *ht = new HashTable();
    
        ht["lee"];
    
        delete ht;
        return 0;
    }
    Error:

    Code:
    c:\Documents and Settings\lee\Desktop\sandbox\sandbox.cpp(46) : error C2107: illegal index, indirection not allowed
    Tis the first time I've had this error. I'm scared.
    Oh I know the hash algorithm is corny and the data array seems a bit pointless, but I have plans, glorious glorious plans!.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    hmm... perhaps you mean to write:
    Code:
    (*ht)["lee"];
    instead?

    Oh, and remember that subscript operators often come in pairs.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by laserlight
    hmm... perhaps you mean to write:
    Code:
    (*ht)["lee"];
    instead?

    Oh, and remember that subscript operators often come in pairs.
    I can't get my head around why the indirection is necessary.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Because ht is a pointer. So to call a member function of the class like operator[], you need to dereference the pointer.

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Oh yeah I see. Sorry I wasn't recalling operator[] is a function.

    Oh yeah you can do this:

    Code:
    ht->operator[]("lee");
    Neat. Well that's the thing I learnt today \/
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21