模板参数中参数1处的C ++类型/值不匹配 [英] C++ type/value mismatch at argument 1 in template parameter
问题描述
好的,所以我在下面有这段代码,当我执行它时,出现以下错误:
Ok so I have this code below and when I execute it I get the following error:
type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Alloc> class std::vector’
vector<s> v;
template <class T>
class A {
public:
struct s{T x;};
};
template <class T>
class B: public A<T> {
public:
using A<T>::s;
vector<s> v;
};
有人可以解释这个问题吗?
Can someone please explain the problem.
推荐答案
问题是编译器不知道 s
是类型还是值.在这种情况下,您添加 typename
或 template
,但是在我测试时,这两种方法都不起作用.直接在 vector
可以使用完整类型:
The issue is that the compiler doesn't know whether s
is a type or a value. This is the case where you add typename
or template
, but neither of those worked when I tested. Using the full type directly in the vector
does work however:
template <class T>
class B: public A<T> {
public:
using A<T>::s;
std::vector<typename A<T>::s> v;
};
(编辑)继续使用它是因为为什么不这样做,当然在我没有尝试过的地方需要 typename
: using
行.以下代码也可以.
(Edit) Kept playing with it because why not, and of course typename
was needed in the one place I didn't trying it: the using
line. The following code also works.
template <class T>
class B: public A<T> {
public:
using typename A<T>::s;
std::vector<s> v;
};
这篇关于模板参数中参数1处的C ++类型/值不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!