Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Follow us on Facebook Follow us on Twitter Linked In Flickr Watch us on YouTube Google+
Results 1 to 2 of 2
  1. #1
    Junior Member Antst's Avatar
    Join Date
    Jul 2010
    Posts
    24
    Downloads
    0
    Uploads
    0

    C++ newbie: 2D array subscripting--problem passing array[i][j] AND array(i,j)...

    ...to function? Hi everyone:

    I would be very grateful for some advice on a problem I'm having with 2-D array subscripting.

    The problem is that some of my arrays are subscripted as array(i, j) (these were created in a class) and some are dynamic arrays subscripted as array[i][j]. I cannot pass both kinds of arrays as arguments to my functions.

    Is there a fix for this? In the array class that produces array(i,j), I tried overloading my subscripting definition, but this did not work. In other words, I currently have:

    T& operator ( ) ( int x, int y );
    const T& operator( ) ( int x, int y ) const;

    And I tried overloading by adding a second definition for []. This produced so many errors that I won't even try to write them all down here.

    Does anyone know of a way to pass arrays as arguments to functions, where some of the arrays are subcripted as array[i][j] and some are array(i,j)?

    Thank you very much for any help.

  2. #2
    Member Oops's Avatar
    Join Date
    Apr 2008
    Posts
    47
    Downloads
    0
    Uploads
    0
    Overload a function. One that takes an normal array, and one that takes your class. If you want to overload a double [][] operator in your class, there is a way to do it. You give your class a data member that has operator [](like a normal array, or a vector). Then you overload the operator to return one of those. For example:

    class Matrix
    {
    ****vector<vector<int> > matrix_; // a vector of vectors of ints
    public:
    ****Matrix(int x, int y) :matrix_(y) {
    ********for(int i=0; i<y; i++)
    ************matrix_[i].resize(x);
    ****}

    ****vector<int> & operator[](int index) { return matrix_[index]; }
    };

    int main()
    {
    ****Matrix m(4,4);
    ****m[1][2] = 7;
    }


Similar Threads

  1. Replies: 2
    Last Post: 10-21-2010, 06:19 PM
  2. Newbie JavaScript Array question. Please help?
    By unibumps in forum Introductions
    Replies: 0
    Last Post: 02-28-2010, 05:10 AM
  3. Replies: 0
    Last Post: 12-07-2009, 05:08 AM
  4. Replies: 0
    Last Post: 12-07-2009, 05:07 AM
  5. Replies: 0
    Last Post: 12-01-2009, 05:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -4. The time now is 03:38 AM.
Powered by vBulletin® Version 4.2.5
Copyright © 2024 vBulletin Solutions Inc. All rights reserved.