【问题标题】:Error in making Template with Forward Declaration of functions in C++在 C++ 中使用前向声明函数制作模板时出错
【发布时间】:2021-11-29 09:39:20
【问题描述】:

我有下面的类代码,当函数的工作完全在类本身中定义时,它没有给出错误

#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 文章是

【问题讨论】:

  • 你的班级和std::vector 会发生冲突,尤其是当using namespace std; 时。不要那样做。也就是说,请将其减少为minimal reproducible example
  • 请更具体
  • 这里有很多代码不需要重现问题。

标签: c++ class oop templates forward-declaration


【解决方案1】:

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

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

您的代码还有其他问题,您可以从遇到的错误中看出这一点,但我们将重点关注这一问题。

【讨论】:

  • 请提供完整代码,因为我是初学者,遇到问题
  • 所提出的问题已得到有效回答。为其余代码中的其他错误添加进一步的解决方案可能会导致答案混乱并降低其对未来提问者的有用性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-24
相关资源
最近更新 更多