繁体   English   中英

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

[英]invalid conversion from 'const float*' to 'float*' in c++

我有以下问题。 我定义了称为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]在编译过程中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
}

}

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

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注释或检查此链接

暂无
暂无

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

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