![](/img/trans.png)
[英]ERR: Constexpr variable must be initialized by a constant expression, but it is
[英]constexpr variable must be initialized by a constant expression
我正在使用 Embarcadero Technology 的 C++Builder 10.3 進行編程,它使用 c++17 編譯器。 我沒有編寫下面的代碼,它比我習慣使用的要復雜一些。 我以前從未使用過 constexpr 。
在下面代碼的底部,當編譯器命中時,“constexpr array g_PERT_S1”會給出錯誤“constexpr variable 'g_PERT_S1' must be initialized by a constant expression. non-constexpr function 'data' cannot be used in a constant expression array (226):在此處聲明“您能否建議更改代碼以解決此錯誤。
struct AAPLUS_EXT_CLASS ELPMPP02PertubationsCoefficient
{
double m_S;
double m_C;
array<int, 13> m_I;
};
constexpr array<ELPMPP02PertubationsCoefficient, 2> g_PERT_S3_4
{ {
{ -5.458720424980e-07, 2.801517894073e-07, { 0, 0, 2, 0, 0, -18, 16, 0, 0, 0, 0, 0, 0 } },
{ -5.121329506146e-07, -2.627345838573e-07, { 0, 0, 0, 0, 0, 18, -16, 0, 0, 0, 0, 0, 0 } }
} };
struct AAPLUS_EXT_CLASS ELPMPP02Pertubations
{
const ELPMPP02PertubationsCoefficient* m_pTable;
size_t m_nTableSize;
};
/*constexpr variable 'g_PERT_S1' must be initialized by a constant expression
non-constexpr function 'data' cannot be used in a constant expression array(226): declared here*/
constexpr array<ELPMPP02Pertubations, 4> g_PERT_S1 /*ERROR HERE*/
{ {
{ g_PERT_S1_1.data(), g_PERT_S1_1.size() },
{ g_PERT_S1_2.data(), g_PERT_S1_2.size() },
{ g_PERT_S1_3.data(), g_PERT_S1_3.size() },
{ g_PERT_S1_4.data(), g_PERT_S1_4.size() }
} };
確實很明確(我假設“array”確實是“std::array”
https://en.cppreference.com/w/cpp/container/array ,
並且您在某處有一個 using namespace std ...
constexpr g_PERT_S1 只能接受用 constexpr 構建的東西。
所以 g_PERT_S1_1 到 g_PERT_S1_4 之一不是 constexpr。
但正如評論中所指出的,您的示例並不完整,因為我們沒有這 4 個定義。
std::array 的 size() 和 data() 成員 function 在 C++17 中是 constexpr,但顯然只有當數組是 constexpr 時它們才會返回 constexpr。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.