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

时间:2019-07-24 11:06:59

标签: c++ pointers

我有以下问题。我定义了一个名为Mux的C ++类

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],并且在Test构造函数中的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
}

}

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

1 个答案:

答案 0 :(得分:-1)

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

如果您确实要执行此操作,则必须使用const_cast

例如const_cast<float*>(&m_ErrVal)。或者,您也可以根据需要选择删除const限定符。


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

有关使用const_cast的更多信息,您可以阅读 @MaxLanghof 注释或选中this link