Overloaded [] with matrix class problem

This is a discussion on Overloaded [] with matrix class problem within the C++ Programming forums, part of the General Programming Boards category; I am trying to create a matrix class but am having a small problem. Everything works just fine except that ...

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    Overloaded [] with matrix class problem

    I am trying to create a matrix class but am having a small problem. Everything works just fine except that I want to be able to do something like this:
    Code:
    matrix myMatrix(3,3);
    myMatrix[0][0]=0;
    This gives me a non-l value error and I'm not sure how to get around this. Here are my classes:
    Code:
    class array
    {
    public:
    	array(int);
    	int operator [] (int index) const {return vec[index];}
    
    private:
    	vector<int> vec;
    };
    
    array::array(int num)
    {
    	for (int x=0; x<num; x++)
    		vec.push_back(0);
    }
    
    class matrix
    {
    public:
    	matrix(int, int);
    	array operator [] (int index) const {return vec[index];}
    
    private:
    	vector<array> vec;
    };
    
    matrix::matrix(int xVal, int yVal)
    {
    	array myArray(yVal);
    	for (int x=0; x<xVal; x++)
    		vec.push_back(myArray);
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    read this. and this and then consider a slight redesign.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Thanks, that helped! I guess its time for a redesign, shouldn't be too hard. Too bad I can't use the []'s like that though, much more intuitive for me at least.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by PJYelton
    Thanks, that helped! I guess its time for a redesign, shouldn't be too hard. Too bad I can't use the []'s like that though, much more intuitive for me at least.
    It can be done, excect that as SC hinted, it isnt as efficient

    A variation on what you did is done via Proxy Classes.....it's presented by Scott Meyers in one of his books......I did an example here - http://www.cprogramming.com/cboard/s...ighlight=Proxy

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Ok, I looked at what you wrote Fordy and I changed my overloaded []'s to return addresses and took away the const's and it worked just fine. I'll probably still redo it to make it more efficient, but its nice to see things done in different ways!

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. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. matrix class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-28-2001, 04:57 PM

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