gpt4 book ai didi

c++ - 错误 : invalid conversion from 'int' to 'const char*' [-fpermissive]

转载 作者:行者123 更新时间:2023-12-02 11:09:13 37 4
gpt4 key购买 nike

我遇到了一个错误,我不知道为什么。

我有一个子类:(标题)

    class motionSensor: public sensorLeaf
{
public:
motionSensor(const int& sensorID, const int& sensorType, bool sensorActivate);

继承自父类(super class)“sensorLeaf”

子类有 2 个私有(private)变量
const float minDistance;
const float maxDistance;

在我的类(class)文件中;这是我的构造函数:
    motionSensor::motionSensor(const int &sensorID, const int &sensorType, bool sensorActivate)
:sensorLeaf(sensorID, sensorType, sensorActivate), minDistance{1.0f}, maxDistance{5.0f}

但是一旦我编译它,我就会收到以下错误:
/home/jb/EmergencySensor/motionsensor.cpp:8: error: invalid conversion from 'int' to 'const char*' [-fpermissive]
:sensorLeaf(sensorID, sensorType, sensorActivate), minDistance{1.0f}, maxDistance{5.0f}
^

Sensorleaf 构造函数是:
sensorLeaf::sensorLeaf(const int sensorID, const std::string sensorType, bool sensorActivate)
{
_sensorID = sensorID;
_sensorType = sensorType;
_sensorActivate = sensorActivate
}

作为私有(private)变量:
private:  
int _sensorID;
std::string _sensorType;
bool _sensorActivate;

};

在做了一些研究之后,我发现当您将字符串定义为 ' ' 而不是 ""时可能会发生错误,但我没有在任何地方使用它。

最佳答案

您正试图强制 const int &sensorType来自 motionSensor构造函数到 const std::string sensorTypesensorLeaf构造函数。

 motionSensor::motionSensor(const int &sensorID, const int &sensorType, bool sensorActivate)
------------------------------------------------------------^
:sensorLeaf(sensorID, sensorType, sensorActivate), minDistance{1.0f}, maxDistance{5.0f}
-----------------------^

sensorLeaf::sensorLeaf(const int sensorID, const std::string sensorType, bool sensorActivate)
-------------------------------------------------------------^

关于c++ - 错误 : invalid conversion from 'int' to 'const char*' [-fpermissive],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33212043/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com