Invalid use of template-name ‘Matrix’ without an argument list

24,485

Solution 1

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.

Solution 2

You should write as:

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

Yes, it's tedious. And it might not be a good idea to put the template definitions in the source file, refer to http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12

So the easiest way is to put the definitions of the template members inside the class template definition in header file.

Share:
24,485
thameera
Author by

thameera

Passionate programmer

Updated on October 22, 2020

Comments

  • thameera
    thameera over 3 years

    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?

  • thameera
    thameera over 12 years
    Where should the above code be? I changed like that in Matrix.cpp above and it still gives the error. :S
  • Eric Z
    Eric Z over 12 years
    Can you give us the definition of Matrix, i.e., "Matrix.h" so that we can take a look?
  • iammilind
    iammilind over 12 years
    @thameera, what error it is giving. Have you #include Matrix.cpp file as I suggested in my answer ? For templates you have to show al declaration and definition to the code which is using it. That's why people try to make template definition within the class body.
  • thameera
    thameera over 12 years
    I've edited the question with Matrix.h, Matrix.cpp and the errors. Any idea what's wrong?
  • thameera
    thameera over 12 years
    I've edited the question with Matrix.h, Matrix.cpp and the errors. Any idea what's wrong?
  • Admin
    Admin over 12 years
    @thameera: What is wrong is a default argument in definition - it can go only with declarations.
  • iammilind
    iammilind over 12 years
    @thameera, see the edited answer. The problem seems to be with default argument.