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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:06:06+00:00 2026-06-14T18:06:06+00:00

I have a Matrix class. I am overloading the multiplication operator, but it’s working

  • 0

I have a Matrix class. I am overloading the multiplication operator, but it’s working only if I call Matrixscalar; isn’t working for scalarMatrix. How can I fix this?

#include <iostream>
#include <stdint.h>

template<class T>
class Matrix {
public:
    Matrix(unsigned rows, unsigned cols);
    Matrix(const Matrix<T>& m);
    Matrix();
    ~Matrix(); // Destructor

Matrix<T> operator *(T k) const;

    unsigned rows, cols;
private:
    int index;
    T* data_;
};

template<class T>
Matrix<T> Matrix<T>::operator *(T k) const { 
    Matrix<double> tmp(rows, cols);
    for (unsigned i = 0; i < rows * cols; i++)
        tmp.data_[i] = data_[i] * k;

    return tmp;
}

template<class T>
Matrix<T> operator *(T k, const Matrix<T>& B) {
    return B * k;
}

Edited


I implemented what chill suggested, but I’m getting the following error:

main.cpp: In function ‘int main(int, char**)’:
main.cpp:44:19: error: no match for ‘operator*’ in ‘12 * u2’
main.cpp:44:19: note: candidate is:
lcomatrix/lcomatrix.hpp:149:11: note: template<class T> Matrix<T> operator*(T, const Matrix<T>&)
make: *** [main.o] Error 1
  • 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-14T18:06:07+00:00Added an answer on June 14, 2026 at 6:06 pm

    Define an operator* outside the class, which just reverses the arguments. And declare the other operator* as const.

    template<typename T> Matrix<T> operator* (T k, const Matrix<T> &m) { return m * k; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a rather large class Matrix , and I've overloaded operator== to
I have some problem about operator overloading. I looked everywhere but couldn't find a
I am working on an assignment that introduces me to operator overloading. I have
Basically, I have a matrix class like this (with a lot of operator overloads
I have a code base, in which for Matrix class, these two definitions are
I have the following class: template <typename T> class matrix { private: int _n;
I have a class that wraps logic for matrix management. Meanwhile I have a
I am working on the equals() method for my sparse matrix class that I'm
I have a matrix class that takes the row and column reference type that
I have been making a matrix class (as a learning exercise) and I have

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.