我有下面的類代碼,當函式的作業在類本身中完全定義時,它不會給出錯誤
#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;
}
但是,當我在類外定義函式并對函式進行前向宣告時,它會給出錯誤 給出錯誤的代碼如下
#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 文章是
- 模板函式的前向宣告
- 前向宣告和模板函式錯誤
uj5u.com熱心網友回復:
當您以這種方式定義建構式時:
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;
}
}
您的代碼還有其他問題,正如您從收到的錯誤中看到的那樣,但我們將重點關注這個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/312593.html
上一篇:創建C 物件的模板類的新實體
下一篇:我如何做可變引數的可變引數模板
