假設我有以下模板化 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。我不明白這個錯誤的原因是什么。誰能向我解釋為什么編譯器會報告此錯誤?
uj5u.com熱心網友回復:
這是嵌套在模板類中的型別的常見錯誤。您需要添加typename以告訴編譯器這Point是一種型別。
...
public:
B(typename A<NO_LUT_POINTS>::Point const (&table)[NO_LUT_POINTS]) : lut(table){}
...
但是,除了解決您的問題之外,請注意它Point不依賴于 的模板引數A,因此您不應將其嵌套在該類中。這將消除添加的必要性typename。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/459135.html
下一篇:Java錯誤:不能應用于給定型別
