C++ 模板名称 'Matrix' 的无效使用没有参数列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7844198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:35:15  来源:igfitidea点击:

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

c++templatescompiler-errors

提问by thameera

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

这是我的 Matrix.cpp 文件。(有一个单独的 Matrix.h 文件)

#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

错误:模板名称“Matrix”的无效使用没有参数列表

What's the problem in my code?

我的代码有什么问题?

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

编辑:类 Matrix 定义为 template<class T>

EDIT: Here's my Matrix.h file:

编辑:这是我的 Matrix.h 文件:

#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:

这是 Matrix.cpp 文件:

#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)'

Matrix.cpp:12:43: 错误:为 'Matrix::Matrix(int, int, T)' 的参数 3 给出默认参数 Matrix.h:16:3: 错误:在 'Matrix::Matrix(整数,整数,T)'

What is wrong in my code?

我的代码有什么问题?

回答by iammilind

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.

另外,不要忘记在使用此类的地方包含此 Cpp 文件。因为在模板的情况下,所有翻译单元都应该可以看到全文。

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 templatedefinition as shown above and the error shall disappear.

您不应该在方法的定义中提供默认参数。在声明中给出(你已经给出了)就足够了。让你的template定义如上图所示与误差应消失。

回答by Eric Z

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

是的,这很乏味。将模板定义放在源文件中可能不是一个好主意,请参阅 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.

所以最简单的方法是将模板成员的定义放在头文件中的类模板定义中。