0

这是我的代码:

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

我不知道如何声明这个类main并使用它。在这种情况下,我有这个错误:

在“a”之前缺少模板参数</p>

当我写:

template(typename T)
   A a(10);

我给出这个错误:

g++ -std=c++11 -c main.cpp 错误:块范围模板中不能出现模板声明^~~~~~~~

4

2 回答 2

1

由于您没有为 编写构造函数A,我想您想使用继承的构造函数,因此您必须提供以下行A

using List<T>::List;

既然你用过c++11,你必须提供模板arg,如下

A<int> a(10);

如果您想让编译器弄清楚,请使用c++17orc++20并提供以下指南

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

现在完整的代码c++17将是

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

c++11将是

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);
}
于 2020-05-21T02:02:21.683 回答
0
std::vector 

是模板化的,所以你称之为std::vector<int>例如。因此,您的声明应该是A<int> a(10);

于 2020-05-21T01:36:35.440 回答