Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7517677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:31:40+00:00 2026-05-30T01:31:40+00:00

I am attempting to create an overloaded operator for a matrix class that I

  • 0

I am attempting to create an overloaded operator for a matrix class that I have built. My matrix class stores the matrix in a dynamically allocated multidimensional array. I am simply trying to test my overloaded operator by multiplying two matrices together that are exactly the same and display the output. I am getting weird results and I believe it has to do with one of the conditionals on my for loops. I have however walked through all of my for loops and can find nothing wrong. The matrices I am multiplying together are both 6×6.

My overloaded operator

template <typename T>
const matrix<T> matrix<T>::operator * (const matrix& right) const
{
    matrix<T> c = right;
    int sum_elems;
    for( int i = 0; i < this->rows - 1; ++i)
    {
        for(int j = 0; j < right.cols - 1; ++j)
        {
            sum_elems = 0;
            for( int k = 0; k < right.rows - 1; ++k)
            {
                sum_elems += this->the_matrix[i][k] * right.the_matrix[k][j];
            }

            c.the_matrix[i][j] = sum_elems;
        }
    }
    return c;
}                 

Now my call to the overloaded operator in my main function:

std::cout << my_matrix;
matrix<int> copy_matrix;
copy_matrix = my_matrix * my_matrix; 
std::cout << copy_matrix;

My output:

  The Matrix:
 0  1  0  1  1  0
 1  0  1  0  1  1
 0  1  0  1  0  1
 1  0  1  0  1  0
 1  1  0  1  0  1
 0  1  1  0  1  0
   The Matrix:
 -1  33  139587680  18  38  75
 139587680  18  38  75  157  1
 139587712  38  1470  4365  10411  1
 139587744  75  4365  19058932  64514866  0
 139587776  157  10411  64514866  1136204102  1
 139596144  1  1  0  1  0

As you can see it seems that I am stepping out of bounds on one of my arrays. I can not seem to find where though. I appreciate your help in advance.

Edit: As requested my full implementation of my matrix class

Matrix Definitions:

template <typename T>
class matrix
{
    public:

        //Default Constructor
        matrix();

        //Overloaded Constructor
        matrix(std::ifstream&, const char*);

        //Copy Constructor
        matrix(const matrix&);

        //Destructor
        ~matrix();


        //overloaded operators
        T* operator [] (T);
        const matrix operator * (const matrix&) const;
        matrix& operator = (const matrix&);
        friend std::ostream& operator << <T> (std::ostream&, const matrix<T>&); 


    private:
        T** the_matrix;
        unsigned rows, cols;

Matrix Implementation:

/* Template version of matrix class */
/*---------------------------------------------------------------------------*/
// Default contructor
template <typename T>
matrix<T>::matrix() { }

// Overloaded contructor
template <typename T>
matrix<T>::matrix( std::ifstream& in, const char* file)
{

    // declare the variables to be used
    T vertices, edges, u, v;
    std::string line;

    // open file for reading
    in.open(file);

    // get number of vertices
    in >> vertices;


    // throw away second line   
    std::getline(in, line);
    std::getline(in, line);

    // get number of edges and dump them in two arrays
    in >> edges;
    T edge1 [edges];
    T edge2 [edges];
    int j = 0, k = 0;
    for(int a = 0; a < edges; ++a)
    {    
        in >> u >> v;
        edge1[j] = u;
        edge2[k] = v;
        ++j;
        ++k;
    }

    in.close();

    // Create multi-dim-dynamic array
    rows = vertices;
    cols = vertices;

    the_matrix = new T*[rows];

    for( int b = 0; b < rows; ++b)
    {
        the_matrix[b] = new T [rows];
    }

    // Initialize array values to zero
    for ( int c = 0; c < rows; ++c)
    {
       for( int d = 0; d < cols; ++d)
       {
           the_matrix[c][d] = 0;
       }
    }

    // push the edges to the matrix
    for( int e = 0; e < edges; ++e)
    {
        the_matrix[edge1[e] - 1][edge2[e] - 1] = 1;
    }
    for ( int f = 0; f < edges; ++f)
    {
        the_matrix[edge2[f] - 1][edge1[f]-1] = 1;
    }


}

// Copy Constructor
template <typename T>
matrix<T>::matrix(const matrix& left)
{
    the_matrix = left.the_matrix;
    rows = left.rows;
    cols = left.cols;
    spath = left.spath;
}

// Destructor
template <typename T>
matrix<T>::~matrix()
{ 
    // Deletes the data in reverse order of allocation
    for( int a = cols; a > 0; --a)
    {
        delete[ ] the_matrix[a];
    }

    delete[ ] the_matrix;
}

// Overloaded * Operator
template <typename T>
const matrix<T> matrix<T>::operator * (const matrix& right) const
{
    matrix<T> c = right;
    T sum_elems;
    for( int i = 0; i < this->rows - 1; ++i)
    {
        for(int j = 0; j < right.cols - 1; ++j)
        {
            sum_elems = 0;
            for( int k = 0; k < right.rows - 1; ++k)
            {
                sum_elems += this->the_matrix[i][k] * right.the_matrix[k][j];
            }

            c.the_matrix[i][j] = sum_elems;
        }
    }
    return c;
}

// Overloaded assignment Operator
template <typename T>
matrix<T>& matrix<T>::operator = (const matrix& right)
{
    this->the_matrix= right.the_matrix;
    this->rows = right.rows;
    this->cols = right.cols;
    this->spath = right.spath;
    return *this;
}

// Overloaded << operator
template <typename T>
std::ostream& operator << (std::ostream& output, const matrix<T>& left)
{
    // Test screen output to see if correct   
    std::cout << std::setw(14) << "The Matrix:" << '\n';
    for( int a = 0; a < left.rows; ++a)
    {
        for( int b = 0; b < left.cols; ++b)
        {
            std::cout << ' ' << left.the_matrix[a][b] << ' ';
        }
        std::cout << '\n';
    }
    return output;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:31:42+00:00Added an answer on May 30, 2026 at 1:31 am

    As I suspected, your copy constructor and assignment operator are in fact not implemented correctly. You are simply copying the pointer over. That means that when you copy one matrix to another, they both share the same data. When one of them goes out of scope, the destructor is called, then the shared data is deleted, leaving the remaining matrix with dangling pointers.

    Fix those functions so they actually allocate new arrays, and copy the data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to create a custom calendar control that inherits from ASP.Net's built in
I am attempting to create an overloaded unary - operator but can't get the
I am attempting to create an Expression that will invoke a specific generic overloaded
I'm attempting to create a Parcelable class in Android so that I can pass
I'm attempting to create a generic controller, ie: public class MyController<T> : Controller where
I am attempting to create a web page that will allow a user to
I'm attempting to create a 100% width toolbar. This toolbar needs to have a
I am attempting to create a wcf service that accepts any input (Action=*) and
I am attempting to create a simple application using Eclipse GEF that displays a
I am attempting to create a MySQL snippet that will analyse a table and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.