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 8879823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:02:10+00:00 2026-06-14T20:02:10+00:00

I am facing a little problem that I can’t solve on my own. I

  • 0

I am facing a little problem that I can’t solve on my own. I am writing a program which implements very simple operations on matrixes. The problem is that when I am trying to do something like this:

Matrix first(5);
Matrix e;

first.identityMatrix();
e = first;
cout << first;
cout << e;

Short explanation: I want to assign square matrix to a matrix without dimensions.

Second cout doesn’t show anything. But when I change Matrix e() to Matrix e(5), everything works perfectly. I know that the bug resides in this piece of code:

Matrix& Matrix::operator=(const Matrix& tmp)
{
if (this->a==0 && this->b == 0)
{
    this->matrix = new double*[tmp.a];

    for (int i=0;i<tmp.a;i++)
        this->matrix[i] = new double[tmp.b];
} else {
    try {
        if (this->a!=tmp.a || this->b!=tmp.b)
            throw wrongSize();
    } catch (wrongSize& e) {
        e.message();
        throw;
    }
}

for (int i=0;i<tmp.a;i++)
{
    for (int j=0;j<tmp.b;j++)
    {
        this->matrix[i][j] = tmp.matrix[i][j];
    }
}

return *this;
}

After some attempts I guess that something is wrong with memory allocation, but I am not sure. To me it ought to work correctly due to the fact that I return the reference to the current object. I think that constructors might be useful as well:

Matrix::Matrix()
{
a = 0;
b = 0;
matrix = NULL;
}

Matrix::Matrix(int a)
{
try {
    if (a==0)
        throw wrongRowOrColNumber();
} catch (wrongRowOrColNumber& e) {
    e.message();
    throw;
}
this->a = a;
this->b = a;
this->matrix = new double*[a];

for (int i=0;i<a;i++)
    matrix[i] = new double[a];
for (int i=0;i<a;i++)
    for (int j=0;j<a;j++)
        matrix[i][j] = 0;
}

Matrix::Matrix(int a, int b)
{
try {
    if (a==0 || b==0)
        throw wrongRowOrColNumber();
} catch (wrongRowOrColNumber& e) {
    e.message();
    throw;
}

if (a==b)
{
    try {
        if (a==0)
            throw wrongRowOrColNumber();
    } catch (wrongRowOrColNumber& e) {
        e.message();
        throw;
    }
    this->a = a;
    this->b = a;
    this->matrix = new double*[a];

    for (int i=0;i<a;i++)
        matrix[i] = new double[a];
    for (int i=0;i<a;i++)
        for (int j=0;j<a;j++)
            matrix[i][j] = 0;
} else {
    this->a = a;
    this->b = b;
    this->matrix = new double*[a];

    for (int i=0;i<a;i++)
        matrix[i] = new double[b];
    for (int i=0;i<a;i++)
        for (int j=0;j<b;j++)
            matrix[i][j] = 0;
}
}

Operator <<:

friend ostream& operator<<(ostream& buffer, const Matrix& tmp)
{
    for (int i=0;i<tmp.a;i++)
    {
        for (int j=0;j<tmp.b;j++)
        {
            buffer << tmp.matrix[i][j] << " ";
        }
        buffer << endl;
    }
    return buffer;
};

IdentityMatrix:

Matrix& Matrix::identityMatrix()
{
try {
    if (this->a!=this->b)
    {
        throw wrongSize();
    }
} catch (wrongSize& e) {
    e.message();
    throw wrongSize();
}
int row = this->a;

for (int i=0;i<row;i++)
{
    for (int j=0;j<row;j++)
    {
        if (i==j)
            this->matrix[i][j] = 1;
        else
            this->matrix[i][j] = 0;
    }
}

return *this;
}
  • 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-14T20:02:12+00:00Added an answer on June 14, 2026 at 8:02 pm

    You throw an exception several times and catch it immediately after, just to show a message and rethrow. You can save the try/catch, if you just show the message and then throw the exception instead.

    In your assignment operator, you must copy the dimensions a and b as well.

    Matrix& Matrix::operator=(const Matrix& tmp)
    {
        if (this->a==0 && this->b == 0)
        {
            this->a = tmp.a;
            this->b = tmp.b;
    
            this->matrix = new double*[tmp.a];
            ...
        }
        ...
    }
    

    In your constructor Matrix::Matrix(int a, int b), you have an if (a == b) ... else. You can remove the if part and just leave the else part. This way, you have less code and less potential for bugs.

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

Sidebar

Related Questions

I am facing a problem that Application.DoEvents() can solve. The problem is that WebBrowser
I'm facing a little problem I can't seem to solve. The problem is in
I am facing a situation that I have problem understanding... I am writing a
I'm facing an architecture problem I didn't manage to solve. I'm developing a little
here is the problem case i am writing a little third party library. In
I'm writing a library¹ in F# that provides some bitwise operations, and I want
I'm facing a quite little problem. In my view.html.erb, I'm using words with German
I am facing a little annoying design problem. Not easy to give a title
I've been having a little problem with getting my program to follow records through
I'm facing a little problem. I have a virtual OS in VMWare and 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.