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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:26:22+00:00 2026-06-03T20:26:22+00:00

I have some problem about operator overloading. I looked everywhere but couldn’t find a

  • 0

I have some problem about operator overloading. I looked everywhere but couldn’t find a proper solution for this error. Here is some parts of my code :

Matrix<type> Matrix<type>::operator/(const Matrix& denom){

if(num_of_rows != denom.num_of_rows || num_of_cols != denom.num_of_cols)
    throw string("Unable to divide (Different size).");
if(denom.contains(0))
    throw string("Unable to divide (Divide by zero).");

for(int i = 0; i < num_of_rows; i++)
    for(int j = 0; j < num_of_cols; j++)
        values[i][j] /= denom.values[i][j]; 
                    // I KNOW THIS IS NOT HOW TO DIVIDE TWO MATRICES

return *this;
 }

 void Matrix<type>::operator=(const Matrix& m) const {

delete [][] values;
num_of_rows = m.num_of_rows;
num_of_cols = m.num_of_cols;
values = new type*[num_of_rows];

for(int i = 0; i < num_of_rows; i++){
    *(values + i) = new type[num_of_cols];
    for(int j = 0; j < num_of_cols; j++)
        values[i][j] = m.values[i][j];
}
 }

And this is the Matrix class and the constructor takes 2 arguments :

class Matrix{

private:
    type** values;
    int num_of_rows, num_of_cols;

public:
    Matrix(){}
    Matrix(int, int);
    type getElement(int, int);
    void print();
    bool contains(type);
    Matrix<type> operator/(const Matrix&);
    void operator=(const Matrix&) const;
};

template <class type>

Matrix<type>::Matrix(int rows, int cols){

values = new type*[rows];
num_of_rows = rows;
num_of_cols = cols;

for(int i = 0; i < rows; i++){
    *(values + i) = new type[cols];
    for(int j = 0; j < cols; j++){
            type random = (type)rand() / 3276.71;
        values[i][j] = random;
    }
}
}

And this piece of code in main gives this error :

srand(time(NULL));
Matrix<int> m1(3,5);  // creating some objects 
Matrix<double> m2(3,5);  // matrices’ elements are assigned randomly from 0 to 10
Matrix<double> m3(5,5);
Matrix<double> m4(5,6);
if(!(m2.contains(0))){
    Matrix<double> m8(3,5);
    m8=m1/m2; // THIS LINE GIVES ERROR
    m8.print();
}
  • 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-03T20:26:24+00:00Added an answer on June 3, 2026 at 8:26 pm

    m1 has type Matrix<int>, so when looking up for a suitable overload of operator/ we find:

    Matrix<int> Matrix<int>::operator/(const Matrix& denom);
    

    Note that the parameter type Matrix here makes use of the so-called injected class name. That means Matrix stands in this case for Matrix<int>, since that’s the (template) class in question. However m2, the argument to the call to operator/, has type Matrix<double>. There is no suitable conversion from Matrix<double> to Matrix<int> so the call is invalid.

    A possible fix is to change operator/ to also be a template:

    // Declared inside Matrix<type>:
    template<typename Other>
    Matrix& operator/=(Matrix<Other> const& other);
    

    (I also took the liberty of fixing the operator to better reflect what it’s actually doing.)

    However you’ll then run into the problem that you’re using a Matrix<int> (the result of the call to operator/) to assign to m8 which has type Matrix<double>. So perhaps you need an operator= to do a conversion (in which case I’d recommend a converting constructor as well, or perhaps even just a converting constructor without a converting operator=).

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

Sidebar

Related Questions

I have some problem to find oop information about my problem in scala, peraphs
I have some problem to order an array by a field of this, here
I'm confused about friend operator overloading. It has no problem if I write the
Problem with operator[] in c++, i have some class: 197 class Permutation{ 198 private:
I'm thinking about a problem which has some similarity with perfect forwarding, but where
We have some performance problems with one of our applications. I thought about using
I have some problem with my IIS today. I try to load a .img
I have some problem with my code public IQueryable<PageItems> GetPageById(Guid Id) { var xml
I have some problem with Jquery Autocomplete plugin ( http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ ) I got it
I have some problem with my .htaccess file. Here I am adding my problem.

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.