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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:36:55+00:00 2026-05-27T15:36:55+00:00

I am developing a matrix class for my personal library. I am using a

  • 0

I am developing a matrix class for my personal library. I am using a template class (Class T) because I want a matrix to be of any numeric or boolean format (and don’t want to retype everything). So what I would like to do is have the operater+= allow a scalar or a matrix. However, I would like to be able to add two matrices together regardless of their numeric format (Class U).

I get the following compile errors.

error: prototype for 'JecMatrix<T>& JecMatrix<T>::operator+=(const JecMatrix<U>&)' does not match any in class 'JecMatrix<T>'
error: candidates are: template<class T> template<class U> JecMatrix& JecMatrix::operator+=(const U&)
error:                 template<class T> template<class U> JecMatrix& JecMatrix::operator+=(const JecMatrix<U>&)

Does anyone have a solution to this? I’ve included the entire class below.

#ifndef JECMATRIX_H
#define JECMATRIX_H

#include <typeinfo>

#include <QTime>
#include <QList>

#include <JecLibrary_global.h>
#include <JecUtils.h>

template<class T>
class JECLIBRARYSHARED_EXPORT JecMatrix
{
public:
    JecMatrix();
    JecMatrix(const int& rows, const int& cols);
    JecMatrix(const QList<QList<T> >& sourceMatrix);

    ~JecMatrix();

    JecMatrix<T>& operator=(const JecMatrix<T>& rhs);
    bool operator!=(const JecMatrix<T>& rhs) const;
    bool operator==(const JecMatrix<T>& rhs) const;

    template<class U> JecMatrix<T>& operator+=(const JecMatrix<U> &rhs) throw(QString);
    template<class U> JecMatrix<T>& operator+=(const U& rhs) throw(QString);

};

template<class T>
JecMatrix<T>::JecMatrix()
{
    T var;
    assertNumber(var);
}

template<class T>
JecMatrix<T>::JecMatrix(const int &rows, const int &cols)
{
    for (int r = 0; r < rows; ++r)
    {
        matrix.append(QList<T>());
        for (int c = 0; c < cols; ++c)
        {
            matrix[r].append(0);
        }
    }
}

template<class T>
JecMatrix<T>::JecMatrix(const QList<QList<T> >& sourceMatrix)
{
    for (int r = 0; r < sourceMatrix.length(); ++r)
    {
        matrix.append(QList<T>());
        for (int c = 0; c < sourceMatrix.at(r).length(); ++c)
        {
            matrix[r].append(sourceMatrix.at(r).at(c));
        }
    }
}

template<class T>
JecMatrix<T>& JecMatrix<T>::operator=(const JecMatrix<T>& rhs)
{
    if (this == &rhs)
    {
        return *this;
    }


    this->id = rhs.id;                          // The id of the Base.

    this->minerals = rhs.minerals;              // The minerals available at that base.
    this->vespene = rhs.vespene;                        // The gas available at that base.

    this->buildings = rhs.buildings;            // The units the player owns during this second. The outer QList holds the types of units and the inner holdes the unique coppies of that unit.
    this->units = rhs.units;                    // The units the player owns during this second. The outer QList holds the types of units and the inner holdes the unique coppies of that unit.

    return *this;
}


template<class T,class U>
JecMatrix<T>& JecMatrix<T>::operator+=(const JecMatrix<U> &rhs) throw(QString)
{
    // Perform type checking.
    U var;
    assertNumber(var);

    // Perform size checking.
    if (rhs.getRows() != getRows()) {
        throw ("To add the matrices they must have the same number of rows and columns.  Matrix a has "
                    + QString::number(a.getRows())
                    + " rows and matrix b has "
                    + QString::number(b.getRows()) + " rows.");
    }

    if (rhs.getCols() != getCols()) {
        throw ("To add the matrices they must have the same number of rows and columns.  Matrix a has "
                    + QString::number(a.getCols())
                    + " cols and matrix b has "
                    + QString::number(b.getCols()) + " cols.");
    }

    double result[][] = new double[a.getRows()][a.getCols()];

    // Add the matrices.
    for (int resultRow = 0; resultRow < a.getRows(); resultRow++) {
        for (int resultCol = 0; resultCol < a.getCols(); resultCol++)
        {
            matrix[resultRow][resultCol] += rhs.matrix[resultRow][resultCol];
        }
    }

    return *this;
}

template<class T, class U>
JecMatrix& JecMatrix::operator+=(const U& rhs) throw(QString)
{
    // Perform type checking.
    U var;
    assertNumber(var);

    // Perform the scalar addition.
    for (int r = 0; r < matrix.length(); ++r)
    {
        for (int c = 0; c < matrix.at(0).length(); ++c)
        {
            matrix[r][c] += rhs;
        }
    }
}


#endif // JECMATRIX_H

Edit: removed irrelevant code.

  • 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-27T15:36:55+00:00Added an answer on May 27, 2026 at 3:36 pm

    Say it like this:

    template <class T>
    template <class U>
    JecMatrix<T>& JecMatrix<T>::operator+=(const JecMatrix<U> &rhs) throw(QString)
    {
        // ...
    }
    

    The two template declarations have to be separate.

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

Sidebar

Related Questions

I'm developing class to represent special kind of matrix: type DifRecord = record Field:
im developing windows application using c#. i want to close the application and show
Currently am developing a program that solves (if possible) any given labyrinth of dimensions
This is my first post here, therefore apologize for any blunders. I'm developing a
We are developing a top-down RPG using XNA. Recently we bumped into a setback
Developing an application for Android, i want to record data that will be usefull
Imagine that I have a matrix of 2x2 or 3x3 pictures and I want
I am developing some linear algebra code that that is templated on the matrix
I am developing an app which has 9 image views in a 3x3 matrix.
here's my question, i'm developing a personal website that has a huge animation in

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.