"<<" operator overloading

This is a discussion on "<<" operator overloading within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <cstring> using namespace std; class Matrix{ private: int i,j; public: int mat[5][5]; Matrix(int x,int y,int p){ ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    11

    "<<" operator overloading

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    class Matrix{
    	private:
    		int i,j;
    	public:
    		int mat[5][5];
    		Matrix(int x,int y,int p){
    			for(i=0;i<x;i++)for(j=0;j<y;j++)mat[i][j]=p;
    		}
    };
    int main(){
    	Matrix m(5,5,1);
    	cout << m(2,2) << endl;
    	return 0;
    }
    Hello all ... i want to show the element of mat[2][2] using cout (<<) operator overloading.How can i do this ??

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    Re : "<<" operator overloading

    Hi,
    As I understand your question you don't need any operater overloading. Just use a method to get mat[i][j] value. you can pass the i & j as parameters.

    Shanuka

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You're going about this class the wrong way. The mat array should be private and you should overload the () operator instead to return the value at the specified index.

    C++ [] array operator with multiple arguments? - Stack Overflow

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    11
    @ Shanuka : I dont want to change "cout << m(2,2) << endl" ... so i need operator overloading.
    @ Memloop : would you please give an example ??

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Check out the link above.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Google functors.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    11
    Thanks for your help
    need some help again

    I have initialized a 3x4 matrix using "Matrix b(3,3)".Here 'b' is an object of Matrix class.now i want to use "b(2,2)=1" this statement to place 1 at that matrix's [2][2] index.how can i do this ??

    example:
    Matrix b(3,3)
    0 0 0
    0 0 0
    0 0 0

    b(2,2)=1
    0 0 0
    0 1 0
    0 0 0
    Thanks in advance

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Make operator() return a reference to int.

    BTW, in case you haven't realized, this is a very awkward way of doing things.

    I'm guessing you are used to languages like Matlab where matrix indexing is done by (). The equivalent for that in C++ is [].

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    11
    Code:
    		double operator()(size_t i,size_t j)const
    		{
    			if(i>=0 && i<length_of_row && j>=0 && j<length_of_col)	return _Matrix[i][j];
    			else throw MatrixError(1);
    			
    		}
    i am using this code portion to print an element of a specified index ...
    cout << b(2,2) << endl will work ...

    what about b(2,2)=1 ??

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Add a & before operator.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by cyberfish View Post
    Add a & before operator.
    That, and make a non-const version of the function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is "<<"
    By arya6000 in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 08:27 AM
  2. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM
  3. Overloaded operator "<<"
    By alphaoide in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2003, 08:47 AM

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