我有下面的类代码,当函数的工作在类本身中完全定义时,它不会给出错误
#include <iostream>
using namespace std;
template <class user_defined_variable>
class vector
{
int size;
public:
user_defined_variable *arr;
vector(int si = 1, bool choice = false)
{
arr = new user_defined_variable[size];
size = si;
if (choice)
{
cout << "Constructor is called! and size of array is " << size << endl;
}
}
user_defined_variable sum_vector()
{
user_defined_variable sum = 0;
for (int i = 0; i < size; i++)
{
sum += this->arr[i];
}
return sum;
}
};
int main()
{
vector<float> vec_1(3);
vec_1.arr[0] = 5.6;
vec_1.arr[1] = 2.12;
vec_1.arr[2] = 3.004;
cout << vec_1.sum_vector() << endl;
return 0;
}
但是,当我在类之外定义函数并对函数进行前向声明时,它会给出错误给出Error的代码如下
#include <iostream>
using namespace std;
template <class user_defined_variable>
class vector
{
int size;
public:
user_defined_variable *arr;
vector(int, bool);
user_defined_variable sum_vector();
};
vector::vector(int si = 1, bool choice = false)
{
arr = new user_defined_variable[size];
size = si;
if (choice)
{
cout << "Constructor is called! and size of array is " << size << endl;
}
}
user_defined_variable vector::sum_vector()
{
user_defined_variable sum = 0;
for (int i = 0; i < size; i++)
{
sum += this->arr[i];
}
return sum;
}
int main()
{
vector<float> vec_1(3);
vec_1.arr[0] = 5.6;
vec_1.arr[1] = 2.12;
vec_1.arr[2] = 3.004;
cout << vec_1.sum_vector() << endl;
return 0;
}
错误是
class_with_error.cpp:16:1: error: invalid use of template-name 'vector' without an argument list
vector::vector(int si = 1, bool choice = false)
^~~~~~
class_with_error.cpp:16:1: note: class template argument deduction is only available with -std=c++17 or -std=gnu++17
class_with_error.cpp:6:7: note: 'template<class user_defined_variable> class vector' declared here
class vector
^~~~~~
class_with_error.cpp:25:1: error: 'user_defined_variable' does not name a type
user_defined_variable vector::sum_vector()
^~~~~~~~~~~~~~~~~~~~~
class_with_error.cpp: In function 'int main()':
class_with_error.cpp:39:26: error: no matching function for call to 'vector<float>::vector(int)'
vector<float> vec_1(3);
^
class_with_error.cpp:12:5: note: candidate: 'vector<user_defined_variable>::vector(int, bool) [with user_defined_variable = float]'
vector(int, bool);
^~~~~~
class_with_error.cpp:12:5: note: candidate expects 2 arguments, 1 provided
class_with_error.cpp:6:7: note: candidate: 'constexpr vector<float>::vector(const vector<float>&)'
class vector
^~~~~~
class_with_error.cpp:6:7: note: no known conversion for argument 1 from 'int' to 'const vector<float>&'
class_with_error.cpp:6:7: note: candidate: 'constexpr vector<float>::vector(vector<float>&&)'
class_with_error.cpp:6:7: note: no known conversion for argument 1 from 'int' to 'vector<float>&&'
如果你知道答案,请帮忙一些参考StackOverflow文章
- 模板函数前向声明
- 正向声明和模板函数错误
当你这样定义构造函数时:
vector::vector(int si = 1, bool choice = false)
{
arr = new user_defined_variable[size];
size = si;
if (choice)
{
cout << "Constructor is called! and size of array is " << size << endl;
}
}
你需要提供模板和它的参数:
template <class user_defined_variable>
vector<user_define_variable>::vector(int si = 1, bool choice = false)
{
arr = new user_defined_variable[size];
size = si;
if (choice)
{
cout << "Constructor is called! and size of array is " << size << endl;
}
}
你的代码还有其他问题,你可以从你得到的错误中看到,但我们只关注这一个。