C ++没有构造函数的实例与参数列表匹配,使用模板
C++ no instance of constructor matches the argument list, using templates
我正在尝试实例化我的 sarray
class。
sarray array(10);
但我收到一条错误消息,指出它们不是具有匹配定义的构造函数。
构造函数在sarray头文件中定义
#ifndef SARRAY_H
#define SARRAY_H
template <class T>
class sarray {
public:
template<class T>sarray(int size);
~sarray();
template<class T> T& operator[](int i);
private:
int size;
T* data;
};
并在sarray cpp文件中实现:
template<class T> sarray::sarray(int size)
: size(size)
{
if (size > 0) data = new T[size];
else {
cout << "illegal array size = " << size << endl;
exit(1);
}
}
我是否需要在构造函数调用中指定我正在使用模板?构造函数参数和调用参数匹配,所以我对错误消息感到困惑。
VS 语法错误信息
C++ argument list for class template is missing
完整源代码
#include "sarray.h"
#include <iostream>;
using namespace std;
template<class T> sarray::sarray(int size)
: size(size)
{
if (size > 0) data = new T[size];
else {
cout << "illegal array size = " << size << endl;
exit(1);
}
}
sarray::~sarray()
{
delete[] data;
}
template<class T> T& sarray::operator[](int i)
{
if (i < 0 || i >= size) {
cout << "index " << i << " is out of bounds." << endl;
exit(1);
}
return data[i];
}
你上面的template<class T>
class会影响整个class。你不需要在class的所有方法前一直添加template<class T>
。当您在 class 之外编写它们时,您确实需要在方法的定义中执行此操作。所以你的 class 应该变成:
template <class T>
class sarray {
public:
sarray(int size);
~sarray();
T& operator[](int i);
private:
int size;
T* data;
};
然后方法的定义如下所示:
template<class T> sarray<T>::sarray(int size)
: size(size)
{
if (size > 0) data = new T[size];
else {
cout << "illegal array size = " << size << endl;
exit(1);
}
}
template<class T> sarray<T>::~sarray()
{
delete[] data;
}
template<class T> T& sarray<T>::operator[](int i)
{
if (i < 0 || i >= size) {
cout << "index " << i << " is out of bounds." << endl;
exit(1);
}
return data[i];
}
但是,正如人们评论的那样,在单独的 .cpp
文件中定义模板 class 的方法并不是一个好主意。您可以在互联网上四处寻找完整的解释,因为这里太长了。
如果您仍希望它们不在 class' 声明中(但仍在同一头文件中),例如更好的可读性,那么你可以按照所示的方式来做。
我正在尝试实例化我的 sarray
class。
sarray array(10);
但我收到一条错误消息,指出它们不是具有匹配定义的构造函数。
构造函数在sarray头文件中定义
#ifndef SARRAY_H
#define SARRAY_H
template <class T>
class sarray {
public:
template<class T>sarray(int size);
~sarray();
template<class T> T& operator[](int i);
private:
int size;
T* data;
};
并在sarray cpp文件中实现:
template<class T> sarray::sarray(int size)
: size(size)
{
if (size > 0) data = new T[size];
else {
cout << "illegal array size = " << size << endl;
exit(1);
}
}
我是否需要在构造函数调用中指定我正在使用模板?构造函数参数和调用参数匹配,所以我对错误消息感到困惑。
VS 语法错误信息
C++ argument list for class template is missing
完整源代码
#include "sarray.h"
#include <iostream>;
using namespace std;
template<class T> sarray::sarray(int size)
: size(size)
{
if (size > 0) data = new T[size];
else {
cout << "illegal array size = " << size << endl;
exit(1);
}
}
sarray::~sarray()
{
delete[] data;
}
template<class T> T& sarray::operator[](int i)
{
if (i < 0 || i >= size) {
cout << "index " << i << " is out of bounds." << endl;
exit(1);
}
return data[i];
}
你上面的template<class T>
class会影响整个class。你不需要在class的所有方法前一直添加template<class T>
。当您在 class 之外编写它们时,您确实需要在方法的定义中执行此操作。所以你的 class 应该变成:
template <class T>
class sarray {
public:
sarray(int size);
~sarray();
T& operator[](int i);
private:
int size;
T* data;
};
然后方法的定义如下所示:
template<class T> sarray<T>::sarray(int size)
: size(size)
{
if (size > 0) data = new T[size];
else {
cout << "illegal array size = " << size << endl;
exit(1);
}
}
template<class T> sarray<T>::~sarray()
{
delete[] data;
}
template<class T> T& sarray<T>::operator[](int i)
{
if (i < 0 || i >= size) {
cout << "index " << i << " is out of bounds." << endl;
exit(1);
}
return data[i];
}
但是,正如人们评论的那样,在单独的 .cpp
文件中定义模板 class 的方法并不是一个好主意。您可以在互联网上四处寻找完整的解释,因为这里太长了。
如果您仍希望它们不在 class' 声明中(但仍在同一头文件中),例如更好的可读性,那么你可以按照所示的方式来做。