なぐも かすみ 2022-04-12 14:45 采纳率: 62.5%
浏览 30

c++ 虚继承的编译warning求解

#include <iostream>

#include <string>

using namespace std;

//首先定义一个Sensor类如下

class Sensor
{

protected:
    string sensorId; //传感器ID

    int interval; //采集频率

public:
    Sensor(string sId, int inv)
    {

        sensorId = sId;

        interval = inv;
    }

    void print()
    {

        cout << "sensorId: " << sensorId << endl;

        cout << "interval: " << interval << endl;
    }
};

//定义一个温度传感器类如下:

class TSensor : virtual public Sensor
{

protected:
    double temperature;

public:
    TSensor(string sId, int inv, double st) : Sensor(sId, inv)
    {

        temperature = st;
    }

    void print()
    {

        cout << "sensorId: " << sensorId << endl;

        cout << "interval: " << interval << endl;

        cout << "temperature: " << temperature << endl;
    }
};

//参照TSensor补全HSensor

class HSensor : virtual public Sensor
{

protected:
    double humidity;

public:
    HSensor(string sId, int inv, double hu) : Sensor(sId, inv)
    {

        humidity = hu;
    }

    void print()
    {

        cout << "sensorId: " << sensorId << endl;

        cout << "interval: " << interval << endl;

        cout << "humidity: " << humidity << endl;
    }
};

class THSensor : public TSensor, public HSensor
{

public:
    THSensor() : Sensor("c000", 0), TSensor("c000", 0, 0), HSensor("c000", 0, 0) {}
    THSensor(string sId, int inv, double st, double sh) : Sensor(sId, inv), TSensor(sId, inv, st), HSensor(sId, inv, sh)
    {
    }
    string getSensorId()
    {

        return sensorId;
    }

    double getTemperature()

    {

        return temperature;
    }
    void print()
    {

        cout << "sensorId: " << TSensor::sensorId;

        cout << " interval: " << TSensor::interval;

        cout << " temperature: " << temperature;

        cout << " humidity: " << humidity << endl;
    }
};

class SensorWareHouse
{

private:
    int capacity;

    THSensor *p;

    int size;

public:
    SensorWareHouse(int capacity)
    {

        p = new THSensor[capacity];
        this->capacity = capacity;
        size = 0;
    }

    ~SensorWareHouse()
    {
        delete []p;
        /**请补全**/
    }

    void add(THSensor &s)
    {
        *(p+size) = s;
        size++;
        /**请补全**/
    }

    void print()
    {

        for (int i = 0; i < size; i++)
        {
            p[i].print();
        }
    }

    void print(double t)
    {

        cout << "higher temperature than " << t << " is list as follow:" << endl;

        for (int i = 0; i < size; i++)
        {

            if (p[i].getTemperature()>t)
            {
                p[i].print();
            }
        }
    }

    THSensor *getTHSensor(string sensorId)
    {

        for (int i = 0; i < size; i++)
        {

            if (p[i].getSensorId() == sensorId)
            {
                return &p[i];
                //返回查询到的传感器地址
            }
        }

        return NULL;
    }
};

int main()
{

   THSensor s1("s001", 10, 20, 30);

    THSensor s2("s002", 10, 21, 31);

    SensorWareHouse wh(10);

    wh.add(s1);

    wh.add(s2);

    wh.print();

    double t;

    cout<<"please input max temperature: ";

    cin>>t;

    wh.print(t);

    while(true) {

        string sensorId;

        cout<<"please input your sensorId(quit input s000): ";

        cin>>sensorId;

        if(sensorId == "s000") return 0;

        else {

            THSensor *s = wh.getTHSensor(sensorId);

            if(s != NULL) s->print();

            else {

                cout<<"not find."<<endl;

            }

        }

    }

    return 0;
}

c:\Users\Lenovo\Desktop\CODE_Cpp\Exercise\2.cpp: In member function 'void SensorWareHouse::add(THSensor&)':
c:\Users\Lenovo\Desktop\CODE_Cpp\Exercise\2.cpp:37:7: warning: defaulted move assignment for 'TSensor' calls a non-trivial move assignment operator for virtual base 'Sensor' [-Wvirtual-move-assign]
class TSensor : virtual public Sensor
c:\Users\Lenovo\Desktop\CODE_Cpp\Exercise\2.cpp:63:7: warning: defaulted move assignment for 'HSensor' calls a non-trivial move assignment operator for virtual base 'Sensor' [-Wvirtual-move-assign]
class HSensor : virtual public Sensor

补充:上面的是warning 信息。 我发现了就是 *(p+size) = s; 的问题,但是不知道产生的原因。
想知道warning产生的原因和解决方法(当然这个warning对程序的实现没有影响)。

更新:目前找到了2个解决办法:
1)修改编译器版本回退为c++03
2)添加~Sensor() 析构函数
但是目前 仍不知道内部出现waring原因!

  • 写回答

1条回答 默认 最新

  • 无夜_ 2022-04-12 15:58
    关注

    可为基类显试提供operator=函数分析看看,未实践,方法不一定正确

    评论

报告相同问题?

问题事件

  • 修改了问题 4月12日
  • 修改了问题 4月12日
  • 创建了问题 4月12日

悬赏问题

  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题
  • ¥15 vs2019的js智能提示
  • ¥15 关于#开发语言#的问题:FDTD建模问题图中代码没有报错,但是模型却变透明了
  • ¥15 uniapp的h5项目写一个抽奖动画
  • ¥15 hadoop中启动hive报错如下怎么解决