在 C++ 中使用函数的前向声明制作模板时出错

Error in making Template with Forward Declaration of functions in C++

我有下面的 class 代码,当函数的工作完全在 class 本身

中定义时,它没有给出错误
#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;
}

但是,当我在 class 之外定义函数并进行函数的前向声明时,它给出了错误 给出错误的代码如下

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

知道的请帮忙解答 一些 Whosebug 参考文章是

当您以这种方式定义构造函数时:

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;
    }
}

您的代码还有其他问题,您可以从收到的错误中看出这一点,但我们会重点解决这个问题。