Home > Enterprise >  Error in making Template with Forward Declaration of functions in C
Error in making Template with Forward Declaration of functions in C

Time:10-10

I'm having the below class code and it is not giving Error when functions' work is defined fully in class itself

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

But, when I'm defining functions outside the class and making a Forward Declaration of the functions then it is giving Error The code which is giving Error is as follows

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

And the Error is

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

Please help if you know the answer Some of the reference StackOverflow articles are

CodePudding user response:

When you are defining your constructor this way:

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

You need to provide the template and it's arguments:

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

There are other issues with your code, as you can see from the errors you're getting, but we'll focus on this one.

  • Related