0

here is my code:

#include <iostream>
using namespace std;
template<typename T>
class List
{
    public:
        List(T);
};
template<typename T>
class A: public List<T>
{
    public:
};
int main()
{   //my problem is here...!
    A a(10);
}

I don't know how to declare this class in main and use it. In this case, I have this error:

missing template arguments before ‘a’

and when I write:

template(typename T)
   A a(10);

I give this error:

g++ -std=c++11 -c main.cpp error: a template declaration cannot appear at block scope template ^~~~~~~~

asmmo
  • 6,922
  • 1
  • 11
  • 25

2 Answers2

1

Since u didn't write a constructor for A, I supposed u want to use the inherited one, hence u have to provide the following line in A

using List<T>::List;

And since u used c++11, u have to provide the template arg, as follows

A<int> a(10);

If u want to make the compiler figure it out, use c++17 or c++20 and provide the following guide

template<class T> A(T)-> A<T>;

Now the full code with c++17 will be

template<typename T>
class List
{
public:
    List(T) {}
};
template<typename T>
class A: public List<T>
{
public:
    using List<T>::List;
};
template<class T> A(T)-> A<T>;
int main()
{   //No problem here...!
    A a(10);
}

And with c++11 will be

template<typename T>
class List
{
public:
    List(T) {}
};
template<typename T>
class A: public List<T>
{
public:
    using List<T>::List;
};


int main()
{   //No problem here...!
    A<int> a(10);
}
asmmo
  • 6,922
  • 1
  • 11
  • 25
0
std::vector 

is templated, so you call it as std::vector<int> for example. As such, your declaration should be A<int> a(10);

Jordan
  • 54
  • 1
  • 11