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

  • Home
  • SEARCH
  • 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 8570297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:31:35+00:00 2026-06-11T18:31:35+00:00

for some reason, I seem to be having an exception whenever I call the

  • 0

for some reason, I seem to be having an exception whenever I call the following (assume A,B & C are all matrices, and that no matrix multiplication rules are broken):

c=a*b; 

I have been stepping through my code for hours now, and cannot for the life of me find whats wrong.

any takers? I think it might be an issue with either the allocate() or clear() functions, or the copy constructor/assignment operator.

Thanks in advance!

// matrix.h
#ifndef matrix_H
#define matrix_H
#include <iostream>
#include <cstdlib>
using namespace std;
template <class mType> class matrix {
public:
    matrix() : N(0), M(0), origin(NULL) { /* EMPTY */ }
    matrix(const matrix<mType> &m) {
        if (origin)
                clear();
        origin = new mType* [m.numrows()];
        for (int i=0; i<m.numrows(); ++i)
            origin[i] = new mType[m.numcols()];

    }
    matrix(int n, int m): N(n), M(m), origin(NULL) {
        allocate(n,m);
    }

    ~matrix() {
        clear();
    }

    matrix & operator=(const matrix &rhs) {

        if (this != &rhs) {     //Check to see they're not the same instance

            this->clear();
            this->allocate(rhs.numrows(), rhs.numcols());
            for(int i=0; i<N; ++i)
                for (int j=0; j<M; ++j)
                    this->origin[i][j] = rhs[i][j];
            }

        return *this;
    }

    matrix & operator+=(const matrix &rhs) {
        try {
            if (    this->numrows() != rhs.numrows() ||
                this->numcols() != rhs.numcols() ) 
                throw 1;
        }
        catch (int e)
        {
            cerr << "Error: The addition of two matrices of different demensions is not defined." << endl;
            return *this;
        }

        for(int i=0; i<N; ++i)
            for (int j=0; j<M; ++j)
                this->origin[i][j] += rhs[i][j];
        return *this;
    }


    const matrix operator+(const matrix &rhs) const {
       matrix tmp = *this;     // tmp copy so we can use the += operator
       return (tmp += rhs);     // return answer
    }

    friend const matrix operator*(const matrix &that, const matrix &rhs) {
        try {
            if (    that.numcols() != rhs.numrows() )
                throw 1;
        }
        catch (int e)
        {
            cerr << "Error: matrix Multiplication not defined." << endl;
            return that;
        }
        matrix<mType> returnmatrix(that.numrows(), rhs.numcols());
        int x=0;
        for (int i=0; i<returnmatrix.numrows(); ++i)
            for (int j=0; j<returnmatrix.numcols(); ++j)
                for (int k=0; k < that.numcols(); ++k){
                    cout << (++x)<<endl;
                    returnmatrix[i][j] += that[i][k] * rhs[k][j];}
        cout << "rt" <<endl;    
        return returnmatrix;

     }


    inline int const numrows() const {
        return N;
    }

    inline int const numcols() const {
        return M;
    }


    void allocate(int n, int m) {
        if (origin)
            clear();
    origin = new mType* [n];
    for (int i=0; i<n; ++i)
        origin[i] = new mType[m];
    M=m;
    N=n;        
}
void clear() {
    if (this->origin) {
        for(int i = 0; i < N; i++)
                delete[] origin[i];
        delete this->origin;
    }

    M=N=0; // Reset

    origin=NULL; 
}



mType* operator [] (const int index)  { return origin[index]; }
const mType* operator [] (const int index) const  { return origin[index]; }



friend matrix<mType> operator*( mType factor, const matrix<mType> rhs ) {
    matrix<mType> out(rhs.numrows() , rhs.numcols());       
        for (int i=0; i<rhs.numrows(); ++i) {
            for (int j=0; j<rhs.numcols(); ++j) {
                out[i][j] = rhs[i][j]*factor;
            }
        }
    return out;
}

friend ostream& operator<< (ostream& out, const matrix<mType>& A) {

    if (A.numrows() > 0 && 0 <  A.numcols()) {
        out <<"[";
        for (int j=0; j<A.numcols(); ++j) {
            out << A[0][j] << " ";
        }
        for (int i=1; i<A.numrows(); ++i) {
            out << endl;
            for (int j=0; j<A.numcols(); ++j) {
                out << " " << A[i][j];
            }
        }
        out << "]" <<endl;

    }
    return out;
}

 friend istream& operator>> (istream& in, matrix<mType> &A)  {
    //[3 2 9 1 2 3 4 5]
    //toss first char
    try {
        if (in.get() != '[')
            throw 1;
        int N, M;
        mType tmp;
        in >> N;
        in >> M;

        A = matrix<mType>(N,M);
        for (int i=0; i<N; ++i)
            for (int j = 0; j < M; j++)
            {   
                in >> tmp;
                A[i][j] = tmp;
            }
        in.get();
            in.ignore();
        }
        catch (int e) {
            cerr << "Invalid Input for matrix" << endl;

        }

        return in;
    }


private: 
    int N, M;
    mType ** origin;


};


#endif

REVISED:

// matrix.h
#ifndef matrix_H
#define matrix_H
#include <iostream>
#include <cstdlib>
using namespace std;
template <class mType> class matrix {
public:
matrix() : N(0), M(0), origin(NULL) { /* EMPTY */ }
matrix(const matrix<mType> &m) {

    origin = new mType* [m.numrows()];
    for (int i=0; i<m.numrows(); ++i)
        origin[i] = new mType[m.numcols()];
    for (int i=0; i<N;++i)
        for (int j = 0; j < M; j++)
        {
            origin[i][j] = m[i][j];
        }


}
matrix(int n, int m): N(n), M(m), origin(NULL) {
    allocate(n,m);
    for (int i=0; i<N;++i)
        for (int j = 0; j < M; j++)
        {
            origin[i][j] = 0;
        }
}

~matrix() {
    clear();
}

matrix & operator=(const matrix &rhs) {

    if (this != &rhs) {     //Check to see they're not the same instance

        this->clear();
        this->allocate(rhs.numrows(), rhs.numcols());
        for(int i=0; i<N; ++i)
            for (int j=0; j<M; ++j)
                this->origin[i][j] = rhs[i][j];
        }

    return *this;
}

matrix & operator+=(const matrix &rhs) {
    try {
        if (    this->numrows() != rhs.numrows() ||
            this->numcols() != rhs.numcols() ) 
            throw 1;
    }
    catch (int e)
    {
        cerr << "Error: The addition of two matrices of different demensions is not defined." << endl;
        return *this;
    }

    for(int i=0; i<N; ++i)
        for (int j=0; j<M; ++j)
            this->origin[i][j] += rhs[i][j];
    return *this;
}


const matrix operator+(const matrix &rhs) const {
   matrix tmp = *this;     // tmp copy so we can use the += operator
   return (tmp += rhs);     // return answer
}

friend const matrix operator*(const matrix &that, const matrix &rhs) {
    try {
        if (    that.numcols() != rhs.numrows() )
            throw 1;
    }
    catch (int e)
    {
        cerr << "Error: matrix Multiplication not defined." << endl;
        return that;
    }
    matrix<mType> returnmatrix(that.numrows(), rhs.numcols());
    int x=0;
    for (int i=0; i<returnmatrix.numrows(); ++i)
        for (int j=0; j<returnmatrix.numcols(); ++j)
            for (int k=0; k < that.numcols(); ++k){
                cout << (++x)<<endl;
                returnmatrix[i][j] += that[i][k] * rhs[k][j];}
    cout << "rt" <<endl;    
    return returnmatrix;

 }


inline int const numrows() const {
    return N;
}

inline int const numcols() const {
    return M;
}


void allocate(int n, int m) {
    if (origin)
        clear();
    origin = new mType* [n];
    for (int i=0; i<n; ++i)
        origin[i] = new mType[m];
    M=m;
    N=n;        
}
void clear() {
    if (this->origin) {
        for(int i = 0; i < N; i++)
                delete[] origin[i];
        delete this->origin;
    }

    M=N=0; // Reset

    origin=NULL; 
}



mType* operator [] (const int index)  { return origin[index]; }
const mType* operator [] (const int index) const  { return origin[index]; }



friend matrix<mType> operator*( mType factor, const matrix<mType> rhs ) {
    matrix<mType> out(rhs.numrows() , rhs.numcols());       
        for (int i=0; i<rhs.numrows(); ++i) {
            for (int j=0; j<rhs.numcols(); ++j) {
                out[i][j] = rhs[i][j]*factor;
            }
        }
    return out;
}

friend ostream& operator<< (ostream& out, const matrix<mType>& A) {

    if (A.numrows() > 0 && 0 <  A.numcols()) {
        out <<"[";
        for (int j=0; j<A.numcols(); ++j) {
            out << A[0][j] << " ";
        }
        for (int i=1; i<A.numrows(); ++i) {
            out << endl;
            for (int j=0; j<A.numcols(); ++j) {
                out << " " << A[i][j];
            }
        }
        out << "]" <<endl;

    }
    return out;
}

 friend istream& operator>> (istream& in, matrix<mType> &A)  {
    //[3 2 9 1 2 3 4 5]
    //toss first char
    try {
        if (in.get() != '[')
            throw 1;
        int N, M;
        mType tmp;
        in >> N;
        in >> M;

        A = matrix<mType>(N,M);
        for (int i=0; i<N; ++i)
            for (int j = 0; j < M; j++)
            {   
                in >> tmp;
                A[i][j] = tmp;
            }
        in.get();
        in.ignore();
    }
    catch (int e) {
        cerr << "Invalid Input for matrix" << endl;

    }

    return in;
}


private: 
    int N, M;
    mType ** origin;


};


#endif
  • 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-06-11T18:31:36+00:00Added an answer on June 11, 2026 at 6:31 pm

    Quite a lot of code but I just looked at the copy constructor and it has two serious mistakes in it.

    First mistake

    matrix(const matrix<mType> &m) {
        if (origin)
                clear();
    

    origin is uninitialized at this point, so you cannot test it’s value. Just remove these two lines. Remember a constructor initializes a new object, it’s wrong if a constructor is testing the object for what is already there.

    Second mistake

    Your copy constructor doesn’t copy anything! It creates a matrix of the right size but it doesn’t copy the matrix values!

    I would guess the first error is the cause of your crash, the second error will just mean you get garbage results.

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

Sidebar

Related Questions

I've got model that use ExtendAssociations , but for some reason deleteHABTM doesn't seem
For some reason we seem to be seeing lots of 404 errors with all
I really can't seem to grok RequireJS for some reason (having looked at various
For some reason I cannot seem to get a ko.computed observable to compute when
For some reason, I can not seem to change the default position of an
for some reason this code wont work, it doesn't seem to read the javascript.php
This is probably something really simple but for some reason I just can't seem
This is probably very easy to do but for some reason I can't seem
for some reason when I try to call CocoaAsyncSocket's onSocket:didReadData:withTag method, it's failing and
For some reason there's a variable called d that is defined immediately after I

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.