Thread: Operator Overloading problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #13
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Code:
    #pragma once
    #include <vector>
    #include <string>
    #include <sstream>
    #include <deque>
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    class Matrix
    {
    private:
    	string _name;
    	int  _row;
    	int  _col;
    	double ** _mat;
    public:
    	//Constructors
    	Matrix();
    	Matrix::Matrix(const Matrix &m);
    	//Deconstructor
    	~Matrix();
    	//get functions for variables
    	string getName(void);
    	int getRow(void);
    	int getCol(void);
    	double ** getPointer(void);
    	//set functions for variables
    	void setName(string name);
    	void setPointer(double ** mat);
    	void setRow(int row);
    	void setCol(int col);
    	//disp matrix 
    	void dispMatrix(void);
    	//clear matrix
    	void clear(void);
    	//operators needed for overloading
    	Matrix & Matrix::operator = (const Matrix & mMatrix);
    };
    Code:
    #include "Matrix.h"
    //default constructor
    Matrix::Matrix()
    {
    	_name = "Unknown";
    	_row = 0;
    	_col = 0;
    	_mat = NULL;
    }
    //copy constructor call
    Matrix::Matrix(const Matrix &m)
    {
    	_name = m._name;
    	_row = m._row;
    	_col = m._col;
    	_mat = m._mat;
    }
    //destructor call
    Matrix::~Matrix()
    {
    	delete _mat;
    }
    //returns the name of a matrix
    string Matrix::getName(void)
    {
    	return _name;
    }
    //returns the number of rows
    int Matrix::getRow(void)
    {
    	return _row;
    }
    //returns the number of columns
    int Matrix::getCol(void)
    {
    	return _col;
    }
    //returns the pointer of data for a matrix
    double ** Matrix::getPointer(void)
    {
    	return _mat;
    }
    //this sets the name of the string passed to matrixs name
    void Matrix::setName(string name)
    {
    	_name.clear();
    	_name = name;
    }
    void Matrix::setPointer(double ** mat)
    {
    	_mat = mat;
    }
    //set the row to row
    void Matrix::setRow(int row)
    {
    	_row = row;
    }
    //set the col to col
    void Matrix::setCol(int col)
    {
    	_col = col;
    }
    //this displays the matrix to cout
    void Matrix::dispMatrix(void)
    {
    	int i,j;
    	cout << _name << " =" << endl << "\t";
    	for(i=0;i<_row;i++)
    	{
    		for(j=0;j<_col;j++)
    		{
    			cout << _mat[i][j] << "\t";
    		}
    		cout << endl;
    	}
    	cout << endl;
    }
    //clear the current matrix
    void Matrix::clear(void)
    {
    	_name = "";
    	_row = 0;
    	_col = 0;
    	_mat = NULL;
    }
    //assignment opererator 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];
    		}
    	}
    	return *this;
    }
    Code:
    // Check for '[' --> assigning new values to matrix
    					if (c == '[') 
    					{
    						if (pos == -1) 
    						{	
    							getline(cin, restOfLine);
    							setCheck = str2matrix(restOfLine,&temp);
    							switch(setCheck)
    							{
    							case -1:
    								cout << "Improperly formatted matrix" << endl;
    								break;
    							case -2:
    								cout << "Improperly formatted matrix" << endl;
    								break;
    							case -3:
    								cout << "Dimensions don't match" << endl;
    								break;
    							case -4:
    								cout << "Improperly formatted matrix" << endl;
    								break;
    							case 1:
    								temp.setName(cmd);
    								mList.push_back(temp);   // errors here when i push a 2nd time
    								break;
    							}
    						}
    						else
    						{				
    							getline(cin, restOfLine);
    							setCheck = str2matrix(restOfLine,&temp);
    							switch(setCheck)
    							{
    							case -1:
    								cout << "Improperly formatted matrix" << endl;
    								break;
    							case -2:
    								cout << "Improperly formatted matrix" << endl;
    								break;
    							case -3:
    								cout << "Dimensions don't match" << endl;
    								break;
    							case -4:
    								cout << "Improperly formatted matrix" << endl;
    								break;
    							case 1:
    								mList[pos] = temp;
    								break;
    							}
    							
    						}
    					}
    Code:
    //string to matrix form
    int str2matrix(string restOfLine, Matrix * temp)
    {
    	int i,j;
    	string tempString;
    	deque<string> sList;
    	istringstream tempNumString;
    	double tempDBL;
    	int row = 0;
    	int col = 0;
    	int lastCol = 0;
    	double ** _mat;
    	if(*restOfLine.begin() == '[' && restOfLine.back() == ']')
    	{
    		for(string::iterator sIT = restOfLine.begin(); sIT != restOfLine.end(); ++sIT)
    		{
    			switch(*sIT)
    			{
    			case '[':
    				//return a -1 if the open bracket isnot at the beginning
    				if(distance(restOfLine.begin(), sIT) != 0)
    					return -1;
    				break;
    			case ']':
    				//return a -2 if the closed bracket is not at the end
    				if(distance(restOfLine.end(), sIT) == 1)
    					return -2;
    				break;
    			case ' ':
    				//keep on adding to num of columns
    				while(*sIT == ' ')
    				{
    					++*sIT;
    				}
    				break;
    			case ';':
    				if(lastCol == 0)
    				{
    					row ++;
    					lastCol = col;
    					col = 0;
    				}
    				else if(lastCol == col)
    				{
    					row ++;
    					col = 0;
    				}
    				else
    				{
    					//this returns a -3 for an error on invalid number of columns
    					return -3;
    				}
    					break;
    			default:
    				if(*sIT >= 48 && *sIT <= 57)
    				{
    					while(*sIT >= 48 && *sIT <= 57)
    					{
    						tempString.push_back(*sIT);
    						++sIT;
    					}
    					--sIT;
    					sList.push_back(tempString);
    					tempString.clear();
    					col ++;
    				}
    				else
    				{
    				//returns -4 for error on not a number but a character
    					return -4;
    				}
    			}
    		}
    		row++;
    
    		_mat = new double *[row-1];
    		for(i=0;i<row;i++)
    		{
    			_mat[i] = new double [col-1];
    			for(j=0;j<col;j++)
    			{
    				tempNumString.str(sList.front());
    				tempNumString >> tempDBL;
    				sList.pop_front();
    				tempNumString.clear();
    				_mat[i][j] = tempDBL;
    			}
    		}
    		temp->setPointer(_mat);
    		temp->setRow(row);
    		temp->setCol(col);
    		//return a 1 for completed
    		return 1;
    	}
    	else
    		return -2;
    }
    Alright the above code is my entire class header and implementation. Im not going to post all the main code because its too big so above is where i am getting errors. I have heap errors and other memory problems.

    The user should input something like the following
    Code:
    a = [ 1 2 3; 4 5 6]
    Last edited by omGeeK; 04-05-2012 at 01:01 PM.

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