Like Tree4Likes

Operator Overloading problem

This is a discussion on Operator Overloading problem within the C++ Programming forums, part of the General Programming Boards category; Code: class Matrix { private: string _name; int _row; int _col; double ** _mat; public: //class methods }; Code: //sets ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97

    Operator Overloading problem

    Code:
    class Matrix
    {
    private:
    	string _name;
    	int  _row;
    	int  _col;
    	double ** _mat;
    public:
    //class methods
    };
    Code:
    //sets the data of a matrix given a matrix. assignment operator overloading
    Matrix & Matrix::operator = (const Matrix & mMatrix)
    {
    	int i,j;
    	_row = mMatrix._row;
    	_col = mMatrix._col;
    	_name = mMatrix._name;
    	_mat = new double *[_row];
    	for(i=0;i<_row;i++)
    	{
    		_mat[i] = new double [_col];
    		for(j=0;j<_col;j++)
    		{
    			_mat[i][j] = mMatrix._mat[i][j]; //fails here
    		}
    	}
    	return *this;
    }
    why does the code above fail?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It looks okay. How does it fail?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Code:
    // Finds matrix with name n in vector mL; returns -1 if matrix not present
    int findMatrix(vector <Matrix> &mL, string n) 
    {
    	Matrix temp;
    	for(vector<Matrix>::iterator iT = mL.begin(); iT != mL.end(); ++iT)
    	{
    		temp = *iT;  
    		//if strings match use std function called distance to find the number of elements between 
    		//the beginning of a vector and the current itereator pos
    		if(temp.getName() == n)
    			return (int)distance(mL.begin(), iT);
    	}
    	return -1;
    }
    i call the assignment operator above when setting temp to my iterator thats when it heads into the operator overload and fails
    Last edited by omGeeK; 04-04-2012 at 08:29 PM.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Actually, now that I think about it, you should be deleting the previous data in _mat before assigning it new data. But that's not exactly a "failure", just a memory leak.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What does your copy constructor look like?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Code:
    Unhandled exception at 0x01192af2 in ECE264 Lab 6.exe: 0xC0000005: Access violation reading location 0x00000000.
    thats the error well the only thing i noticed is when i look at iT in debug mode the pointer is pointing to 0x00000000 which makes me believe maybe somehow its getting messed up when i push into the vector ill have to look over my other code

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    There's probably something wrong with your copy constructor, which would be needed to push Matrix's onto the vector.

    But why are you using temp in findMatrix? It's an unnecessary copy operation. Just do:
    Code:
    if (iT->getName() == n)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Thanks for the heads up but what do you mean by a copy constructor? All i have is a default constructor

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by omGeeK
    Thanks for the heads up but what do you mean by a copy constructor? All i have is a default constructor
    You have a copy constructor too, since you did not disable it. Generally, whenever you need to define the destructor, copy constructor or the copy assignment operator, you need to define all three.

    Why not use std::vector?
    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

  10. #10
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Vector for what?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by omGeeK
    Vector for what?
    To store the data. This way, you would not need to define the copy constructor, copy assignment operator or destructor, unless you need to do some other special things in them.
    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

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by omGeeK View Post
    Thanks for the heads up but what do you mean by a copy constructor? All i have is a default constructor
    laserlight knows a lot more about this than me, but I believe the way you've currently defined your Matrix you need a copy ctor to provide a "deep copy" of your data.

    A copy ctor has a signature like:
    Code:
    Matrix(const Matrix& matrix);
    and creates a new Matrix by copying the data from an existing matrix. It would be similar to your operator= method (but has no return statement, of course, and doesn't need to first delete any old data).

    If you don't define a copy ctor then you get a default one that does a "shallow copy", simply copying each field, which is not what you want since that would mean that _mat in the copy would point to the same data as the matrix being copied instead of having its own copy of the data.

    However, as laserlight said, if you used a vector inside your matrix to hold the data, then you could use the default copy ctor since a vector has a proper copy ctor defined already.

    Anyway, if you're still having trouble you should post your whole program.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by oogabooga View Post
    However, as laserlight said, if you used a vector inside your matrix to hold the data, then you could use the default copy ctor since a vector has a proper copy ctor defined already.
    @omGeek: Btw, a vector of int* s would still need a copy constructor, but vector of vector<int> s would not.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,662
    Quote Originally Posted by manasij7479 View Post
    @omGeek: Btw, a vector of int* s would still need a copy constructor, but vector of vector<int> s would not.
    A vector<vector<int> > also would not.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  15. #15
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by grumpy View Post
    A vector<vector<int> > also would not.
    What did you think, I meant by vector of vector<int> `s ?
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operator Overloading problem
    By FMan in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2008, 01:37 PM
  2. operator overloading problem
    By salmansalman in forum C++ Programming
    Replies: 3
    Last Post: 08-21-2008, 08:00 AM
  3. Problem with overloading an operator.
    By apacz in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2006, 11:00 AM
  4. operator overloading problem~
    By black in forum C++ Programming
    Replies: 6
    Last Post: 07-20-2004, 10:04 AM
  5. Problem with Operator Overloading
    By bench386 in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 01:39 AM

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