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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:55:20+00:00 2026-05-26T08:55:20+00:00

Here is my Matrix.cpp file. (there’s a separate Matrix.h file) #include <iostream> #include <stdexcept>

  • 0

Here is my Matrix.cpp file. (there’s a separate Matrix.h file)

#include <iostream>
#include <stdexcept>

#include "Matrix.h"

using namespace std;

Matrix::Matrix<T>(int r, int c, T fill = 1)
{
  if (r > maxLength || c > maxLength) {
    cerr << "Number of rows and columns should not exceed " << maxLen << endl;
    throw 1;
  }

  if (r < 0 || c < 0) {
    cerr << "The values for the number of rows and columns should be positive" << endl;
    throw 2;
  }

  rows = r;
  cols = c;

  for (int i = 0; i < rows; i++)
    for (int j = 0; j < cols; j++)
      mat[i][j] = fill;

}

This gives the following

error: invalid use of template-name ‘Matrix’ without an argument list

What’s the problem in my code?

EDIT: The class Matrix is defined with a template<class T>

EDIT:
Here’s my Matrix.h file:

#include <iostream>
#include <math.h>

#define maxLength 10;

using namespace std;

template <class T>

class Matrix
{
public:
    Matrix(int r, int c, T fill = 1);

private:
    int rows, cols;
        T mat[10][10];
};

And here’s the Matrix.cpp file:

#include <iostream>
#include <stdexcept>

#include "Matrix.h"

using namespace std;

template<class T>
Matrix<T>::Matrix(int r, int c, T fill = 1)
{
}

This gives the following error:

Matrix.cpp:12:43: error: default argument given for parameter 3 of
‘Matrix::Matrix(int, int, T)’ Matrix.h:16:3: error: after previous
specification in ‘Matrix::Matrix(int, int, T)’

What is wrong in my 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-26T08:55:20+00:00Added an answer on May 26, 2026 at 8:55 am

    If your class is template then correct definition should be,

    template<class T>
    Matrix<T>::Matrix(int r, int c, T fill)  // don't give default argument
    ...
    

    Also, don’t forget to include this Cpp file where you use this class. Because in the case of templates, full body should be visible to all translation units.

    Edit: After your edited question, I noticed that the error says it all.

    You are not suppose to give the default argument inside the definition of the method. It’s adequate to give in the declaration(which you already gave). Make your template definition as shown above and the error shall disappear.

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

Sidebar

Related Questions

Here's the code i'm using to rotate: Dim m As New System.Drawing.Drawing2D.Matrix Dim size
I am using g++ version 4.4.3 on Ubuntu OS. According to this matrix here
I am loading matrix from file and here comes the question, how to treat
I have a problem with generics in c++.I have two Matrix.h and Matrix.cpp files.Here
I followed these instructions here: http://w3schools.com/razor/razor_example.asp NOTE: I'm using Web Matrix The example said
Here is program for memoized version matrix chain multiplication program from Introduction to Algorithms
Here is my question : I have A = 1xN matrix and B =
Hi here is my problem i am able to input values for my matrix
I have code for a matrix multiplication lab using double[,] and I wanted to
I am trying to create a Matrix class using c++. So far, 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.