简体   繁体   English

C ++初始化类成员变量取决于其他成员变量

[英]c++ initialize class member variable depends on other member variable

Basically, a non-static member theta which initialize by another class member but well initialized. 基本上是一个非静态成员theta ,它由另一个类成员初始化但初始化良好。 Then valley_max initialized by theta as you can see. 然后valley_max通过初始化theta ,你可以看到。 Right now everything works fine. 现在一切正常。 Then I want to initialize a array whose bound is valley_max . 然后,我想初始化一个边界为valley_max的数组。 First, the I got error : 首先,我得到了错误:

invalid use of non-static data member 无效使用非静态数据成员

Then I add static const int valley_max as you can see. 然后,您可以看到添加了static const int valley_max But I got error like: 但是我遇到了类似的错误:

array bound is not an integer constant before ']' token 数组绑定不是']'标记之前的整数常量

I just wondering if I can initialize the array whose bound initialized by a member variable which initialized by another member variables. 我只是想知道是否可以初始化由成员变量初始化的数组,该成员变量由另一个成员变量初始化。
Thanks for your help. 谢谢你的帮助。

AP_Tmxk_VFH.cpp AP_Tmxk_VFH.cpp

AP_Tmxk_VFH::AP_Tmxk_VFH() :
    l(5),
    threshold(5),
    safe_space(0.7),
    detected_range(2.5),
    theta(degrees(acos(1-sq(safe_space)/(2*sq(detected_range))))),
    valley_max(round_float(180.0/theta)),
    valley_count(0),
{
}

AP_Tmxk_VFH.h AP_Tmxk_VFH.h

class AP_Tmxk_VFH {
privte:
       int l;
       int threshold;
       int safe_space;
       int theta;
       int detected_range;
       static const int valley_max ;
       struct{

         bool inside_valley  = false;
         uint16_t up_bound   = 0;  
         uint16_t down_bound = 0; 
   }valley[valley_max];
}

Your specific issue is due to the fact that variable length arrays are not supported in C++. 您的特定问题是由于C ++不支持可变长度数组。 Consider using a std::vector or another C++ standard library container instead. 考虑使用std::vector或其他C ++标准库容器代替。

But you have further problems (which in my opinion makes your question interesting): the order of member initialisation is the order they appear in the class definition, not the order they appear in the initialisation. 但是您还有其他问题 (我认为这使您的问题很有趣):成员初始化的顺序是它们在类定义中出现的顺序, 而不是它们在初始化中出现的顺序。

For example, in your case theta is initialised before detected_range , and since the latter is not initialised at the point you use it in the evaluation of theta , the behaviour of your code is undefined! 例如,在您的情况下, theta detected_range 之前初始化的,并且由于后者不是在您评估theta时在使用时初始化的,因此代码的行为是不确定的!

In your case, unless you need the members to be const , I'd initialise the ones that are not set to literals in the constructor body if I were you. 在您的情况下,除非您需要将成员设为const ,否则如果您是我,则将在构造函数主体中初始化未设置为文字的那些成员。

I just wondering if I can initialize the array whose bound initialized by a member variable which initialized by another member variables. 我只是想知道是否可以初始化由成员变量初始化的数组,该成员变量由另一个成员变量初始化。

No, you can not. 你不能。

You can not, because such variable is 您不能,因为这样的变量是

not an integer constant 不是整数常数

like the error message says. 就像错误消息说的那样。 The value of the member variable is unknown at compile time - which contradicts with the requirement that the size of an array must be compile time constant (ie known at compile time). 成员变量的值在编译时未知-这与数组大小必须为编译时常数(即在编译时已知)的要求相矛盾。

Solution: Use a std::vector instead. 解决方案:改用std::vector The size of a vector is not locked at compile time. 向量的大小在编译时不会锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM