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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:19:29+00:00 2026-05-27T17:19:29+00:00

I have a matrix class like below: template <size_t M, size_t N, typename T>

  • 0

I have a matrix class like below:

template <size_t M, size_t N, typename T>
class Matrix
{
public:
    Matrix<M, N, T> operator +(const Matrix<M, N, T>& B) const;
    template <size_t P> Matrix<M,P,T> operator*(const Matrix<N, P, T>& B) const;
    template <typename T2> operator T2() const;  

private:
  T data[M][N];
};

// ... the body is in header file too  ...//

The body has written fine, and everything works well.
When I define two Matrices as below:

Matrix < 10, 10, int> m1;
Matrix < 10, 10, float> m2;

m1 + m2;  // OK
m1 * m2;  // error: no match for 'operator*' in 'm1 * m2'

The first ‘+’ operator works well, because an implicit casting has performed on it.
but for second ‘*’ operator for different value types, an error occurs.

error: no match for ‘operator*’ in ‘m1 * m2’

Any idea ?!

UPDATE:
All code is in header file. I have no problem but for ‘*’ operator.

What you can say about ‘+’ operator? I know everything about template/operators/casting… but this problem is like a bug for my gcc compiler!? I wrote a cast-operator and this operator calls before ‘+’ operator, but i dont know why it dose not perform for ‘*’ operator!

  • 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-05-27T17:19:30+00:00Added an answer on May 27, 2026 at 5:19 pm

    The problem is more or less classic. The overload resolution starts by
    building a list of possible functions; in this case, functions named
    operator*. To do this, it adds all operator* functions which are in
    scope to the list, and it tries to instantiate all function templates by
    applying type deduction; if type deduction succeeds, it adds the
    instantiation of the template to the list. (A function template is
    not a function. An instantiation of the function template is a
    function.)

    The rules for template type deduction are different than those used in
    overload resolution. In particular, only a very small set of
    conversions are considered. User defined conversion operators are not
    considered. The result is that in m1 * m2, type deduction for
    operator* fails (since it would require a conversion which isn’t
    considered). So no instantiation of the function template is added to
    the list, and there is no other operator*.

    More generally: you’re operator T2() wouldn’t allow type deduction
    even if it were allowed; there are a infinite number of conversions
    which would match operator*. I suspect, in fact, that you’ve made it
    too general; that you want an operator Matrix<M, N, T2>(). (Not that
    this will help here, but there are contexts where it might eliminate an
    ambiguity.)

    You might be able to make it work by defining a:

    template<size_t P, tyepname OtherT>
    Matrix<M, P, T> operator*( Matrix<N, P, T> const& rhs ) const;
    

    , then doing the conversion inside the operator*. (I haven’t tried it,
    and am not sure, but I think your existing operator* should be
    considered “more specialized”, and thus be chosen when type
    deduction succeeds for both.)

    Having said this, I think the way you’re doing it is the wrong approach.
    Do you really want the return types of m1 * m2 and m2 * m1 to be
    different. For starters, I’d require the client code to make the
    conversion explicit (which is the case in your current code); if you do
    want to support the implicit conversions, I think you need to make the
    operator* a global, use some sort of simple meta-programming to
    determine the correct return type (i.e. given Matrices of long and
    unsigned, you might want to have a return type of unsigned long,
    since this is what mixed type arithmetic with these types gives
    otherwise), convert both sides to the target type, and do the arithmetic
    on it. A lot of work for what is probably not a very important or
    useful feature. (Just my opinion, of course. If your clients really
    want the mixed type arithmetic, and are willing to pay for it…)

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

Sidebar

Related Questions

I have the following class: template <typename T> class matrix { private: int _n;
I have a SquareMatrix template class which inherits Matrix template class, like below: SquareMatrix.h:
I have a class template<size_t N, size_t M> class Matrix { // .... };
I have a matrix class defined this way: template<int M, int N, typename T>
I have a 'matrix' object like follows: public class Matrix { public Dictionary<string, string>
Basically, I have a matrix class like this (with a lot of operator overloads
Suppose I have a rather large class Matrix , and I've overloaded operator== to
I have a class which contains a few boost::numeric::ublas::matrix's within it. I would like
I have a template matrix class class defined in a header called Matrix.h. Certain
I have a code base, in which for Matrix class, these two definitions are

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.