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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:38:43+00:00 2026-05-21T09:38:43+00:00

Good morning. I been dealing with this error for 3 days now and I

  • 0

Good morning. I been dealing with this error for 3 days now and I can’t figure it out. I was tasked to create a header file for a series of matrix tests to learn the use of of templates on c++. I seem to have all other operators working except for operator*=. I am including the header file, plus the error I am getting.

#ifndef MATRIX_H
#define MATRIX_H
#include <vector>
#include <iostream>
#include <math.h>
#include <complex>

using namespace std;
namespace nkumath {

template <typename T, size_t ROWS, size_t COLS>
class Matrix {
    friend class Matrix;
public:

    Matrix(const T & init = T()) : elts(ROWS, vector<T>(COLS, init)) {
    };

    const vector<T> & operator[](int ROWS) const {
        return elts[ROWS];
    }; //not sure if correct

    vector<T> & operator[](int ROWS) {
        return elts[ROWS];
    }; //not sure if correct

    //MatrixAdd
    Matrix & matrixAdd(const Matrix & lhs, const Matrix & rhs) {
        for (int r = 0; r < ROWS; r++) {
            for (int c = 0; c < COLS; c++) {
                this->elts[r][c] = lhs[r][c] + rhs[r][c];
            }
        }
        return *this;
    };

    //MatrixSubtract

    Matrix & matrixSubtract(const Matrix & lhs, const Matrix & rhs) {

        for (int r = 0; r < ROWS; r++) {
            for (int c = 0; c < COLS; c++) {
                this->elts[r][c] = lhs[r][c] - rhs[r][c];
            }
        }
        return *this;
    };

    //MatrixMult

    template<size_t INNER>
    Matrix & matrixMult(const Matrix<T, ROWS, INNER> & mat1, const Matrix<T, INNER, COLS> & mat2) {
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                //elts[i][j] = 0;
                for (int k = 0; k < INNER; k++) {
                    elts[i][j] += mat1.elts[i][k] * mat2.elts[k][j];
                }
            }
        }
        return *this;
    }; //not done

    //print function

    void print(ostream & out) const {
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                out << elts[i][j];
            }
            out << "\n";
        }
    };

private:
    vector< vector<T> > elts;
};
//Note, you have to define each time a template to avoid having the errors
//Operator<<

template <typename T, size_t ROWS, size_t COLS>
ostream & operator<<(ostream & out, const Matrix<T, ROWS, COLS> & elts) {
    elts.print(out);
    return out;
};

//Operator==

template <typename T, size_t ROWS, size_t COLS>
bool operator==(const Matrix<T, ROWS, COLS> & lhs, const Matrix<T, ROWS, COLS> & rhs) {
    return true;
};
//Operator+

template <typename T, size_t ROWS, size_t COLS>
Matrix<T, ROWS, COLS> operator+(const Matrix<T, ROWS, COLS> & lhs, const Matrix<T, ROWS, COLS> & rhs) {
    Matrix<T, ROWS, COLS> returnVal;
    return returnVal.matrixAdd(lhs, rhs);
};
//Operator-

template <typename T, size_t ROWS, size_t COLS>
Matrix<T, ROWS, COLS> operator-(const Matrix<T, ROWS, COLS> & lhs, const Matrix<T, ROWS, COLS> & rhs) {
    Matrix<T, ROWS, COLS> returnVal;
    return returnVal.matrixSubtract(lhs, rhs);
};
//Operator*

template <typename T, size_t ROWS, size_t COLS>
Matrix<T, ROWS, COLS> operator*(const Matrix<T, ROWS, COLS> & lhs, const Matrix<T, ROWS, COLS> & rhs) {
    Matrix<T, ROWS, COLS> returnVal;
    return returnVal.matrixMult(lhs, rhs);
};

//Operator+=

template <typename T, size_t ROWS, size_t COLS, typename C>
Matrix<T, ROWS, COLS> operator+=(Matrix<T, ROWS, COLS> & lhs, const C & rhs) {
    //Matrix<T,ROWS,COLS> returnVal;
    lhs.matrixAdd(lhs, rhs);
    return lhs;
};

//Operator-=

template <typename T, size_t ROWS, size_t COLS, typename C>
Matrix<T, ROWS, COLS> operator-=(Matrix<T, ROWS, COLS> & lhs, const C & rhs) {
    lhs.matrixSubtract(lhs, rhs);
    return lhs;
};
//Operator*=

template <typename T, size_t ROWS, size_t COLS, typename C>
Matrix<T, ROWS, COLS> operator*=(Matrix<T, ROWS, COLS> & lhs, const C & rhs) {
    lhs.matrixMult(lhs, rhs);
    return lhs;
};
//Operator/=

template <typename T, size_t ROWS, size_t COLS>
Matrix<T, ROWS, COLS> operator/=(const Matrix<T, ROWS, COLS> & lhs, const int rhs) {
    Matrix<T, ROWS, COLS> returnVal(rhs);
    for (int r = 0; r < ROWS; r++) {
        for (int c = 0; c < COLS; c++) {
            returnVal[r][c] = lhs[r][c] / returnVal[r][c];
        }
    }
    return returnVal;
};
//Operator%=

template <typename T, size_t ROWS, size_t COLS>
Matrix<T, ROWS, COLS> operator%=(const Matrix<T, ROWS, COLS> & lhs, const int rhs) {
    Matrix<T, ROWS, COLS> returnVal(rhs);
    for (int r = 0; r < ROWS; r++) {
        for (int c = 0; c < COLS; c++) {
            returnVal[r][c] = lhs[r][c] % returnVal[r][c];
        }
    }
    return returnVal;
};
    } // namespace Matrix

    #endif // MATRIX_H

here is my main.cpp. I am not allowed to change this file….

// main.cpp
// Test driver for Matrix class template project.
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>  // for rand()
#include "Matrix.h"

using namespace std;
using namespace nkumath;

template <typename T, size_t ROWS, size_t COLS>
void randomize(Matrix<T, ROWS, COLS> & mat)
// Put random values in a Matrix.
// Note:  It must be possible to assign T an int value.
{
for (size_t i = 0; i < ROWS; i++)
    for (size_t j = 0; j < COLS; j++)
        mat[i][j] = (rand() % 21) - 10; // Random number in range -10,...,+10
}

struct Complex
{
Complex(double re = 0.0, double im = 0.0) : real(re), imag(im) { }
Complex & operator+=(const Complex & rhs)
{
    real += rhs.real;
    imag += rhs.imag;
    return *this;
}
Complex & operator-=(const Complex & rhs)
{
    real -= rhs.real;
    imag -= rhs.imag;
    return *this;
}
Complex & operator*=(const Complex & rhs)
{
    real = real * rhs.real - imag * rhs.imag;
    imag = real * rhs.imag + imag * rhs.real;
    return *this;
}
double real;
double imag;
};
Complex operator+(const Complex & lhs, const Complex & rhs)
{
return Complex(lhs.real + rhs.real, lhs.imag + rhs.imag);
}
Complex operator-(const Complex & lhs, const Complex & rhs)
{
return Complex(lhs.real - rhs.real, lhs.imag - rhs.imag);
}
Complex operator*(const Complex & lhs, const Complex & rhs)
{
return Complex(lhs.real * rhs.real - lhs.imag * rhs.imag, lhs.real * rhs.imag + lhs.imag * rhs.real);
}
ostream & operator<<(ostream & out, const Complex & c)
{
out << "(" << c.real << " + " << c.imag << "i)";
return out;
}


int main()
{
srand(100);
ofstream out("output.txt");

// Matrix construction, operator[], and printing:
Matrix<int, 4, 5> m1(2);
out << "m1: " << endl;
m1.print(out);
const Matrix<int, 4, 5> m2 = m1;
out << "m2: " << endl << m2;
for (size_t i = 0; i < 4; i++)
    m1[i][i] = 5;
out << "m1: " << endl << m1;

// Tests of const correctness:
// m2[0][0] = 0; // This line should not compile.
// m2 += 4; // Neither should this one.
int n = m2[0][0]; // This line should be okay.

// Scalar operation tests:
out << "m1 += 4: "  << endl << (m1 += 4);
out << "m1 -= 6: " << endl << (m1 -= 6);
out << "m1 *= 12: " << endl << (m1 *= 12);
out << "m1 /= 2: " << endl << (m1 /= 2);
out << "m1 %= 7: " << endl << (m1 %= 7);

// Matrix addition and subtraction tests:
Matrix<int, 4, 5> m3;
out << "m3: " << endl << m3;
out << "m3.matrixAdd(m1, m2): " << endl << m3.matrixAdd(m1, m2);
out << "m3.matrixSubtract(m1, m2): " << endl << m3.matrixSubtract(m1, m2);
out << "m2 + m1: " << endl << (m2 + m1);
out << "m2 - m1: " << endl << (m2 - m1);

// Matrix multiplication tests:
Matrix<int, 2, 3> m4;
randomize(m4);
out << "m4: " << endl << m4;
Matrix<int, 3, 5> m5;
randomize(m5);
out << "m5: " << endl << m5;
Matrix<int, 2, 5> m6;
out << "m6.matrixMult(m4, m5): " << endl << m6.matrixMult(m4, m5);
Matrix<int, 2, 5> m7;
matrixMult(m4, m5, m7);
out << "m6 == m7: " << (m6 == m7) << endl;
out << "m6 == m4 * m5: " << (m6 == m4 * m5) << endl;

// Matrices of strings:
Matrix<string, 3, 4> m8("Hello");
for (size_t i = 0; i < 3; i++)
    m8[i][i] = "   Hi";
out << "m8: " << endl << m8 << endl;
Matrix<string, 3, 4> m9(" there!");
out << "m9: " << endl << m9 << endl;
out << "m8 + m9: " << endl << m8 + m9 << endl;
Matrix<string, 4, 5> m10(", Goodbye!");
//out << m8 * m10 << endl; // This line should not compile.

// Matrices of Complex:
Matrix<Complex, 2, 8> m11;
randomize(m11);
Complex c(1, -3);
m11 += c;
out << "m11: " << endl << m11 << endl;
Matrix<Complex, 8, 3> m12;
randomize(m12);
m12 -= c;
out << "m12: " << endl << m12 << endl;
out << "m11 * m12: " << endl << m11 * m12 << endl;

out.close();
}

The error I am getting is

Error   1   error C2784: 'nkumath::Matrix<T,ROWS,COLS> &nkumath::Matrix<T,ROWS,COLS>::matrixMult(const nkumath::Matrix<T,4,INNER> &,const nkumath::Matrix<T,INNER,5> &)' : could not deduce template argument for 'const nkumath::Matrix<T,INNER,5> &' from 'const int' e:\documents and settings\pato\my documents\visual studio 2010\projects\thematrix\thematrix\matrix.h    136

I am using visual studio c++ 2010. I tried to compile the header on linux gcc4 and I goth a whole different set of errors, so I am going to finish this project in windows. Either way just need someone to point me to the right direction, I have been looking at this for too long.
Thank you!
PJ

  • 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-21T09:38:43+00:00Added an answer on May 21, 2026 at 9:38 am

    In your main

    1. you were missing the using namespace std and using namespace nkumath declarations
    2. there is a call matrixMult(m4, m5, m7); which cannot resolve. I’m assuming you mean m7 = m4*m5;

    It seems like you have incorrectly made a previously static matrixMult (with the meaning of innerproduct) into a member function of Matrix;

    Using this as a freestanding static function works:

        template<class T, size_t ROWS, size_t INNER, size_t COLS>
        static Matrix<T, ROWS, COLS> innerProduct(const Matrix<T, ROWS, INNER> & mat1, const Matrix<T, INNER, COLS> & mat2) {
            Matrix<T, ROWS, COLS> result;
            for (int i = 0; i < ROWS; i++) {
                for (int j = 0; j < COLS; j++) {
                    //elts[i][j] = 0;
                    for (int k = 0; k < INNER; k++) {
                        result.elts[i][j] += mat1.elts[i][k] * mat2.elts[k][j];
                    }
                }
            }
            return result;
        } //not done
    

    Update

    It means that you should rewrite the matrixMult method like so (using the above static innerProduct helper):

        template<size_t OUTER>
        Matrix<T, ROWS, OUTER> matrixMult(const Matrix<T, COLS, OUTER> & mat2) const {
            return innerProduct(*this , mat2);
        }
    

    also adding something like this is probably what you want:

    template<class T, size_t ROWS, size_t INNER, size_t COLS>
    Matrix<T, ROWS, COLS> operator*(const Matrix<T, ROWS, INNER> & lhs, const Matrix<T, INNER, COLS> & rhs)
        {
            return innerProduct(lhs, rhs);
        }
    

    I haven’t bothered fixing the scalar multiplication, but you should get the idea now (if I’m not wasting my time). Good luck

    This way the main.cpp can stay as it was… capice?


    This is my resulting Matrix class definition for your perusal:

    namespace nkumath {
    
        template <typename T, size_t ROWS, size_t COLS>
        class Matrix {
        public:
    
            Matrix(const T & init = T()) : elts(ROWS, vector<T>(COLS, init)) {
            }
    
            const vector<T> & operator[](int r) const {
                return elts[r];
            } //not sure if correct
    
            vector<T> & operator[](int r) {
                return elts[r];
            } //not sure if correct
    
            //MatrixAdd
            Matrix & matrixAdd(const Matrix & lhs, const Matrix & rhs) {
                for (int r = 0; r < ROWS; r++) {
                    for (int c = 0; c < COLS; c++) {
                        this->elts[r][c] = lhs[r][c] + rhs[r][c];
                    }
                }
                return *this;
            }
    
            //MatrixSubtract
    
            Matrix & matrixSubtract(const Matrix & lhs, const Matrix & rhs) {
    
                for (int r = 0; r < ROWS; r++) {
                    for (int c = 0; c < COLS; c++) {
                        this->elts[r][c] = lhs[r][c] - rhs[r][c];
                    }
                }
                return *this;
            }
    
            //MatrixMult
    
            template<size_t OUTER>
            Matrix<T, ROWS, OUTER> matrixMult(const Matrix<T, COLS, OUTER> & mat2) const {
                return innerProduct(*this , mat2);
            } //not done
    
            //print function
    
            void print(ostream & out) const {
                for (int i = 0; i < ROWS; i++) {
                    for (int j = 0; j < COLS; j++) {
                        out << elts[i][j];
                    }
                    out << "\n";
                }
            }
    
        private:
            vector< vector<T> > elts;
        };
        //Note, you have to define each time a template to avoid having the errors
        //Operator<<
    
            template<class T, size_t ROWS, size_t INNER, size_t COLS>
            static Matrix<T, ROWS, COLS> innerProduct(const Matrix<T, ROWS, INNER> & mat1, const Matrix<T, INNER, COLS> & mat2) {
                Matrix<T, ROWS, COLS> result;
                for (int i = 0; i < ROWS; i++) {
                    for (int j = 0; j < COLS; j++) {
                        //elts[i][j] = 0;
                        for (int k = 0; k < INNER; k++) {
                            result.elts[i][j] += mat1.elts[i][k] * mat2.elts[k][j];
                        }
                    }
                }
                return result;
            } //not done
        template <typename T, size_t ROWS, size_t COLS>
        ostream & operator<<(ostream & out, const Matrix<T, ROWS, COLS> & elts) {
            elts.print(out);
            return out;
        }
    
        //Operator==
    
        template <typename T, size_t ROWS, size_t COLS>
        bool operator==(const Matrix<T, ROWS, COLS> & lhs, const Matrix<T, ROWS, COLS> & rhs) {
            return true;
        }
        //Operator+
    
        template <typename T, size_t ROWS, size_t COLS>
        Matrix<T, ROWS, COLS> operator+(const Matrix<T, ROWS, COLS> & lhs, const Matrix<T, ROWS, COLS> & rhs) {
            Matrix<T, ROWS, COLS> returnVal;
            return returnVal.matrixAdd(lhs, rhs);
        }
        //Operator-
    
        template <typename T, size_t ROWS, size_t COLS>
        Matrix<T, ROWS, COLS> operator-(const Matrix<T, ROWS, COLS> & lhs, const Matrix<T, ROWS, COLS> & rhs) {
            Matrix<T, ROWS, COLS> returnVal;
            return returnVal.matrixSubtract(lhs, rhs);
        }
        //Operator*
    
        template <typename T, size_t ROWS, size_t COLS>
        Matrix<T, ROWS, COLS> operator*(const Matrix<T, ROWS, COLS> & lhs, const Matrix<T, ROWS, COLS> & rhs) {
            Matrix<T, ROWS, COLS> returnVal;
            return returnVal.matrixMult(lhs, rhs);
        }
    
        //Operator+=
    
        template <typename T, size_t ROWS, size_t COLS, typename C>
        Matrix<T, ROWS, COLS> operator+=(Matrix<T, ROWS, COLS> & lhs, const C & rhs) {
            //Matrix<T,ROWS,COLS> returnVal;
            lhs.matrixAdd(lhs, rhs);
            return lhs;
        }
    
        //Operator-=
    
        template <typename T, size_t ROWS, size_t COLS, typename C>
        Matrix<T, ROWS, COLS> operator-=(Matrix<T, ROWS, COLS> & lhs, const C & rhs) {
            lhs.matrixSubtract(lhs, rhs);
            return lhs;
        }
        //Operator*=
    
        template <typename T, size_t ROWS, size_t COLS, typename C>
        Matrix<T, ROWS, COLS> operator*=(Matrix<T, ROWS, COLS> & lhs, const C & rhs) {
            lhs.matrixMult(lhs, rhs);
            return lhs;
        }
        //Operator/=
    
        template <typename T, size_t ROWS, size_t COLS>
        Matrix<T, ROWS, COLS> operator/=(const Matrix<T, ROWS, COLS> & lhs, const int rhs) {
            Matrix<T, ROWS, COLS> returnVal(rhs);
            for (int r = 0; r < ROWS; r++) {
                for (int c = 0; c < COLS; c++) {
                    returnVal[r][c] = lhs[r][c] / returnVal[r][c];
                }
            }
            return returnVal;
        }
        //Operator%=
    
        template <typename T, size_t ROWS, size_t COLS>
        Matrix<T, ROWS, COLS> operator%=(const Matrix<T, ROWS, COLS> & lhs, const int rhs) {
            Matrix<T, ROWS, COLS> returnVal(rhs);
            for (int r = 0; r < ROWS; r++) {
                for (int c = 0; c < COLS; c++) {
                    returnVal[r][c] = lhs[r][c] % returnVal[r][c];
                }
            }
            return returnVal;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good morning, I've been up all night trying to figure this out on my
This has been bugging me all morning and I can't even figure out where
Good morning. I've been having this issue for some days and have been lokking
Good morning people - I've been having this problem for hours and I can't
Good Morning All. I've been struggling with this issue for a while now, and
Good Morning SO! We've been scratching our heads with with this interesting scenario at
Good morning, all. I've been doing websites now for about seven years (most of
I've been working at this all morning and I still can't find a way
Good morning everyone. Wondering if anyone can help me out. I am trying to
Good morning SO- this ones been cooking my noodle for a bit (also, it's

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.