东方既白aaa 2021-08-19 22:39 采纳率: 0%
浏览 33
已结题

vs code配置 c++ delete命令报警

在class computer的析构函数中的三个if语句中的delete会有警告提示(delete called on 'VIDEOCARD' that is abstract but has non-virtual destructor [-Wdelete-non-virtual-dtor]),void test1()中的delete却没有报警。在vs2019中4个delete均没有警告提示,请问是不是我的vscode配置有问题,还请各位指点一下,我应该如何修改vscode的配置


#include <iostream>
#include <string>
using namespace std;
//create abstract base class
class CPU{
     public:
     virtual void process() = 0;//接口
};
class VIDEOCARD{
     public:  
     virtual void display() = 0;
};
class MEMORY{
     public:  
     virtual void storage() = 0;
};
//generate a computer
class computer{
     public://构造函数初始化对象
     computer(CPU *cpu, VIDEOCARD *videocard, MEMORY *memory)//作用是CPU *m_cpu=   new interorlenovo
     {
          cout << "构造函数调用" << endl;
          m_cpu = cpu;
          m_videocard = videocard;
          m_memory = memory;

     }
     ~computer(){
          cout << "析构函数调用" << endl;
          if (m_cpu != NULL)
          {
                  delete m_cpu;                       //这三个if语句中的delete会有报警提示
                  cout << "delete m_cpu done" << endl;
          }
          if (m_videocard != NULL)
          {
               delete m_videocard;
               cout << "delete m_videocard done" << endl;
          }
          if (m_memory != NULL)
          {
               delete m_memory;
               cout << "delete m_memory done" << endl;
          }
     }
     public:
     CPU *m_cpu;
     VIDEOCARD *m_videocard;
     MEMORY *m_memory;
};
//specify factory to generate the part of computer
class intercpu: public CPU{
     public:
     virtual void process(){//具体实现
          cout << "使用inter cpu组装电脑" << endl;
     }
};
class intervideocard: public VIDEOCARD{
     public:
     virtual void display(){//具体实现
          cout << "使用inter 显卡组装电脑" << endl;
     }
};
class intermemory: public MEMORY{
     public:
     virtual void storage(){//具体实现
          cout << "使用inter 内存条组装电脑" << endl;
     }
};

//测试例子
void test0(){
     CPU *Cpu = new intercpu;
     VIDEOCARD *Videocard = new intervideocard;
     MEMORY *Memory = new intermemory;

     computer Computer(intercpu, intervideocard, intermemory);
     Cpu->process(); Videocard->display(); Memory->storage();
     
     // delete 

}
void test1(){
     computer * com = new computer(new intercpu, new intervideocard, new intermemory);
     com->m_cpu->process(); com->m_videocard->display(); com->m_memory->storage();
     delete com; //这里没有报警
}
int main(){

     test0();
     cout << "-————————————————————————————————————" << endl;
     test1();
     return 0;
}





在class computer的析构函数中的三个if语句中的delete会有警告提示(delete called on 'VIDEOCARD' that is abstract but has non-virtual destructor [-Wdelete-non-virtual-dtor]),void test1()中的delete没有警报提示。在vs2019中4个delete均没有警告提示,请问是不是我的vscode配置有问题,还请各位指点一下,我应该如何修改vscode的配置

  • 写回答

2条回答 默认 最新

  • 东方既白aaa 2021-08-20 08:43
    关注

    已经解决了,在相应的类中加入虚析构函数就没有报警,最近刚开始学类,对类的语法还不是很熟悉。但是不明白为什么vscode会报警,而vs2019不报警,希望知道原因的的哥哥姐姐看到这里帮我解答一下,

    评论

报告相同问题?

问题事件

  • 系统已结题 8月27日
  • 创建了问题 8月19日

悬赏问题

  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真
  • ¥15 关于#c语言#的问题,请各位专家解答!