假设我有以下模板化 C++ 类
#include <cstdint>
template <uint32_t NO_POINTS>
class A
{
public:
struct Point
{
float x;
float y;
};
A(const Point (&points)[NO_POINTS])
{
for (uint32_t point = 0; point < NO_POINTS; point++) {
table[point] = points[point];
}
}
private:
Point table[NO_POINTS];
};
我想使用这个类的一个实例作为以下类的私有成员:
#include "A.h"
template <uint32_t NO_LUT_POINTS>
class B
{
public:
B(A<NO_LUT_POINTS>::Point (&table)[NO_LUT_POINTS]) : lut(table){}
private:
A<NO_LUT_POINTS> lut;
};
#include "B.h"
int main(int argc, char** argv) {
B<4> foo({{1326.0, 25.0}, {1601.0, 30.0}, {1922.0, 35.0}, {2293.0, 40.0}});
return 0;
}
我试图编译此代码,但编译器报告以下错误A<NO_LUT_POINTS>::Point is not a type
。我不明白这个错误的原因是什么。谁能向我解释为什么编译器会报告此错误?
这是嵌套在模板类中的类型的常见错误。您需要添加typename
以告诉编译器这Point
是一种类型。
...
public:
B(typename A<NO_LUT_POINTS>::Point const (&table)[NO_LUT_POINTS]) : lut(table){}
...
但是,除了解决您的问题之外,请注意它Point
不依赖于 的模板参数A
,因此您不应将其嵌套在该类中。这将消除添加的必要性typename
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句