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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:56:36+00:00 2026-06-01T12:56:36+00:00

I am struggling to get my head around operator overloading. In this case the

  • 0

I am struggling to get my head around operator overloading. In this case the + operator I have and example of what I have tried, any help would be greatly appreciated.

I am getting an error which says “invaild use of ‘class Matrix’ I am unsure on how to fix this how can I add these two Matrix objects together?

Matrix Matrix::operator+(const Matrix& rhs){
return Matrix(Matrix + rhs.Matrix());
}


   Matrix::Matrix(int MM, int NN){
                  M = MM;
                  N = NN;
                  data = new double[M * N];
                  for ( int i =0; i < M; i++)
                  {
                      for (int j = 0; j < N; j++)
                      {
                          data[i* N+j] = (double) 1000 + i*N+j;
                    //      cout << data[i*N+j] <<"\t";
                      }
                      //cout <<"\n";
                  }

       cout << "Matrix Constructor... (code to be implemented here!!!)\n";}

Thanks

  • 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-01T12:56:37+00:00Added an answer on June 1, 2026 at 12:56 pm

    jpm’s answer is very important to look at. Once you’ve fixed these things, you can look at mine.

    Essentially, an operator overload is no different than any other function.

    So, in the case of:

    Matrix Matrix::operator+(const Matrix& rhs)
    

    What you’re really doing there is saying: add rhs to the current matrix, and return a new matrix. Your overload should not alter the current matrix. Help yourself and use a constant:

    Matrix Matrix::operator+(const Matrix& rhs) const
    

    A matrix addition like that should first check if the matrices have the same dimensions so that you can add them together, then loop through all “cells” and add them, and create a matrix out of that. For that, I’m guessing you’ll need a second constructor, something like:

    Matrix::Matrix(int MM, int NN, double values[])
    {
        M = MM;//TODO: change to width
        N = NN;//TODO: change to height
        data = new double[M*N];
        for(int i < 0; i < M; i++)
            for(int j < 0; j < N; j++)
                data[i * N+j] = values[i * N+j];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am struggling to get my head around how to scale any sort of
i am seriously struggling to get my head around regex. I have a sring
Ok so i've been struggling to get my head around this for a few
Struggling to get my head around these MD Arrays so hoping someone can help.
Ok, so I've been trying to get my head around this, but I'm struggling.
Am learning android and am struggling to get my head around this particular layout
I'm struggling to get my head around LINQ and have come to the conclusion
I'm really struggling to wrap my head around this: I have a UserModel and
I'm struggling to get my head around how to implement UJS in Rails (specifically,
I'm struggling to get my head around when to use a couple of the

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.