在 C++ 中从 'const float*' 到 'float*' 的转换无效



>我有以下问题。我已经定义了C++名为 Mux 的类

class Mux{
public:
    Mux(uint32_t* const bitsArray, const uint32_t control_01, const uint32_t control_02,
        float* input_01, float* input_02, float* input_03, float* input_04,
        float* output);
    virtual ~Mux();
    void Update(void);
private:
    uint32_t* m_BitsArray;
    uint32_t  m_Control01;
    uint32_t  m_Control02;
    float*    m_Input01;
    float*    m_Input02;
    float*    m_Input03;
    float*    m_Input04;
    float*    m_Output;
    uint8_t GetControlValue(uint32_t* const bitsArray, const uint32_t control_01, const uint32_t control_02);
};
Mux::Mux(uint32_t* const bitsArray,
                        const uint32_t control_01, const uint32_t control_02,
                        float* input_01, float* input_02, float* input_03, float* input_04,
                        float* output):
                        m_BitsArray{bitsArray},
                        m_Control01{control_01},
                        m_Control02{control_02},
                        m_Input01{input_01},
                        m_Input02{input_02},
                        m_Input03{input_03},
                        m_Input04{input_04},
                        m_Output{output}{
}
Mux::~Mux() {
    // TODO Auto-generated destructor stub
}
void Mux::Update(void){
    uint8_t controlValue = GetControlValue(m_BitsArray, m_Control01, m_Control02);
    switch(controlValue){
        case 0:
            *m_Output = *m_Input01;
        break;
        case 1:
            *m_Output = *m_Input02;
        break;
        case 2:
            *m_Output = *m_Input03;
        break;
        case 3:
            *m_Output = *m_Input04;
        break;
    }
}
uint8_t Mux::GetControlValue(uint32_t* const bitsArray, const uint32_t control_01, const uint32_t control_02){
    uint8_t controlValue = 0;
    if(Utils::TestBitSet(bitsArray, control_01)){
        controlValue += 1;
    }
    if(Utils::TestBitSet(bitsArray, control_02)){
        controlValue += 2;
    }
    return controlValue;
}

一旦我尝试在另一个名为 Test 的类中实例化这个类我在编译过程中收到error: invalid conversion from 'const float*' to 'float*' [-fpermissive],测试构造函数中 Mux 类构造函数调用中的最后一个参数带有下划线。

class Test{
public:
    Test(float par01Value, float par02Value);
    virtual ~Test();
    void Loop(void);
private:
    uint32_t m_BitsArray[1] = {0};
    Mux *m_MuxTest;
    const float m_ErrVal = 0.0;
    struct Pars_t{
        float par01;
        float par02;
    };
    struct Vars_t{
        float var01;
        float var02;
        float var03;
    };
    Vars_t m_Vars = {0.0};
    Pars_t m_Pars = {0.0};
};

#define LW_01               (0)
// Byte 01
#define LSig01              (LW_01*32 + 0x00)
#define LSig02              (LW_01*32 + 0x01)
//efine L                   (LW_01*32 + 0x02)
//efine L                   (LW_01*32 + 0x03)
//efine L                   (LW_01*32 + 0x04)
//efine L                   (LW_01*32 + 0x05)
//efine L                   (LW_01*32 + 0x06)
//efine L                   (LW_01*32 + 0x07)
// Byte 02
//efine L                   (LW_01*32 + 0x08)
//efine L                   (LW_01*32 + 0x09)
//efine L                   (LW_01*32 + 0x0A)
//efine L                   (LW_01*32 + 0x0B)
//efine L                   (LW_01*32 + 0x0C)
//efine L                   (LW_01*32 + 0x0D)
//efine L                   (LW_01*32 + 0x0E)
//efine L                   (LW_01*32 + 0x0F)
// Byte 03
//efine L                   (LW_01*32 + 0x10)
//efine L                   (LW_01*32 + 0x11)
//efine L                   (LW_01*32 + 0x12)
//efine L                   (LW_01*32 + 0x13)
//efine L                   (LW_01*32 + 0x14)
//efine L                   (LW_01*32 + 0x15)
//efine L                   (LW_01*32 + 0x16)
//efine L                   (LW_01*32 + 0x17)
// Byte 04
//efine L                   (LW_01*32 + 0x18)
//efine L                   (LW_01*32 + 0x19)
//efine L                   (LW_01*32 + 0x1A)
//efine L                   (LW_01*32 + 0x1B)
//efine L                   (LW_01*32 + 0x1C)
//efine L                   (LW_01*32 + 0x1D)
//efine L                   (LW_01*32 + 0x1E)
//efine L                   (LW_01*32 + 0x1F)
Test::Test(float par01Value, float par02Value){
    m_Pars.par01 = par01Value;
    m_Pars.par02 = par02Value;
    m_MuxTest = new Mux(m_BitsArray, LSig01, LSig02,
                                            &m_ErrVal,
                                            &(m_Pars.par01),
                                            &(m_Pars.par02),
                                            &m_ErrVal,
                                            &(m_Vars.var01));
}
void Test::Loop(void){
    m_MuxTest->Update();
}
Test::~Test(){
    // TODO Auto-generated destructor stub
}
}

我不明白我做错了什么。有人可以帮忙吗?

您声明m_ErrValconst float &m_ErrVal因此const float *Mux的构造函数期望的是float *而不是const float *。这是错误。

如果你真的想这样做,你将不得不使用 const_cast .

例如const_cast<float*>(&m_ErrVal) .或者,如果不需要,您可以删除 const 限定符,这是您的选择。


编辑:但是,如果您不打算将给定的float *修改为Mux构造函数,我认为您应该将它们作为const float *传递,而不是使用const_cast

有关使用const_cast的更多信息,您可以阅读@MaxLanghof评论或查看此链接。

最新更新