[] Operator Overload not working

This is a discussion on [] Operator Overload not working within the C++ Programming forums, part of the General Programming Boards category; If I use cout<<stak->operator[](2) It works But If I use cout<<stak[2]<<endl I shows a lot errors from ostream ---------------- error: ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    [] Operator Overload not working

    If I use cout<<stak->operator[](2) It works
    But If I use
    cout<<stak[2]<<endl
    I shows a lot errors from ostream
    ----------------
    error: no match for 'operator<<' in 'std::cout << *(
    and many more
    -----------
    I think this means stak[2] is not returning int
    but stak->operator[](2) is returning
    I cant understand any reasons for this
    can anybody help me out Plese ??
    I am using GCC on Debian
    with KDevelop IDE
    My Full source Code is
    http://rapidshare.com/files/11053475...st.tar.gz.html
    If you are browsing the source Code Look at line 50 and 51 of linkedlist.cpp
    Last edited by noobcpp; 04-26-2008 at 07:22 AM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    try (*stak)[2]

    -> dereferences the left hand side.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Thanks Worked
    But what should my operator ocerload method signature be to use stak[2] simply ?
    My Current One is
    Code:
    int operator[](int);
    Code:
    int array::operator[](int i){
    	return at(i);
    }

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It already works, but stak is a pointer to array, not an array, and therefore you need to dereference it first before you can call the operator[].

    Code:
    int main()
    {
        //array* stak = new array;
        array stak;
        std::cout << stak[0];
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    -> is used to access members on a variable, which clearly stak did not have. Instead you try to access a member of stak named operator <<
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    I need to us New
    array* stak = new array(5);
    and the overload[] must work with pointer to
    what I need to do for that ??

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Arrays and pointers work the same in this regard. Anon's solution works for pointers, as well.
    Last edited by Elysia; 04-26-2008 at 08:06 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Dereferencing is annoying It doesnt look good to use (*stak)[2] instead of stak[2]
    Is it impossible ??
    Last edited by noobcpp; 04-26-2008 at 08:05 AM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Quote Originally Posted by noobcpp View Post
    Dereferencing is annoying It doesnt look good to use (*stak)[2] instead of stak[2]
    Is it impossible ??
    (*stak)[2] is actually equal to (**stak)[2].
    And
    stak[2] is actually equal to (*stak)[2].
    This is how the compiler sees it, but you type the first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    so if I do some Change on
    Code:
    int operator[](int);
    signature of array Class
    would it be possible to use stak[2] if
    Code:
    array* stak = new array();

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    (*stak)[2] is actually equal to (**stak)[2].
    What??? What utter nonsense! There's a level of indirection of difference here.

    You cannot overload operator for types that aren't user-defined. Raw pointers of any kind are not user-defined, thus you cannot overload [] for array*.

    You could just define an alias.
    Code:
    array *pstak = new array(5);
    array &stak = *pstak;
    
    cout << stak[2]
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Elysia how do you figure that?

    edit: beaten a-gain.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    It means that operator [] dereferences the pointer.
    p[0] == *p
    *p[0] == **p
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Quote Originally Posted by Elysia View Post
    It means that operator [] dereferences the pointer.
    p[0] == *p
    *p[0] == **p
    Compile first, kids.
    Code:
    #include <iostream>
    #include <ostream>
    #include <cstddef>
    
    class array
    {
    public:
        explicit array ( std::size_t sz ): p( new int[sz] ), size( sz )
        {
            if ( size <= 0 ) {
                throw length_error();
            }
        }
        ~array ( ) { delete [] p; }
        const int & operator[] ( std::size_t idx ) const  { return p[idx]; }
        int & operator[] ( std::size_t idx ) { return p[idx]; }
        // ...
    private:
        class length_error { };
        int * p;
        size_t size;
    };
    
    int main ( )
    {
        array * stak = new array( 5 );
        std::cout << ( **stak )[0] << std::endl; 
    }
    Compiling...
    learn.cpp
    c:\documents and settings\jck\my documents\visual studio 2005\projects\learn\learn\learn.cpp(27) : error C2100: illegal indirection
    Build log was saved at "file://c:\Documents and Settings\JCK\My Documents\Visual Studio 2005\Projects\learn\learn\Debug\BuildLog.htm"
    learn - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Price is right fail music would be nice here.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    You cannot dereference a pointer more than once, of course. Duh. It works if it's a pointer to pointer.

    Code:
    int main( )
    {
    	int* p;
    	int** p2;
    
    	*p;
    	p[0];
    
    	*p2;
    	**p2;
    	p2[0];
    	*p2[0];
    	p2[0][0];
    }
    Compiles fine.
    Don't run it, though.
    Last edited by Elysia; 04-26-2008 at 08:27 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sudoku solver
    By manav in forum Game Programming
    Replies: 11
    Last Post: 02-03-2008, 10:38 PM
  2. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 08:47 PM
  3. Animation class not working
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 03-02-2005, 06:48 AM
  4. working with file name [] ???
    By rox in forum C++ Programming
    Replies: 5
    Last Post: 03-06-2004, 11:52 AM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 05:08 AM

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