zoukankan      html  css  js  c++  java
  • 关于编译错误(1)

    /home/ztteng/work/test/20130814/inc/PartList.h: In constructor ‘CarPart::CarPart(int, int)’:
    /home/ztteng/work/test/20130814/inc/PartList.h:21: warning: ‘CarPart::itsModelYear’ will be initialized after
    /home/ztteng/work/test/20130814/src/PartList.cpp:10: warning:   base ‘Part’
    /home/ztteng/work/test/20130814/src/PartList.cpp:9: warning:   when initialized here

    #include <iostream>
    using namespace std;
    
    class Part
    {
    private:
        int itsPartNumber;
    public:
        Part():itsPartNumber(1){}
        Part(int PartNumber):itsPartNumber(PartNumber){}
        virtual ~Part(){}
    
        int GetPartNumber() const {return itsPartNumber;}
        virtual void Display() const =0;
    };
    
    
    class CarPart : public Part
    {
    private:
        int itsModelYear;
    public:
        CarPart():itsModelYear(94){}
        CarPart(int year,int partNumber);
        virtual void Display() const
        {
            Part::Display();
            cout<<"Model Year: ";
            cout<<itsModelYear<<endl;
        }
        
    };

    构造函数实现的时候

    CarPart::CarPart(int year, int partNumber):Part(partNumber),itsModelYear(year)
    {}

      

      其实g++给的这个warning就是为了提醒你,初始化列表中数据成员的初始化顺序应该和你在类的声明中给出数据成员的顺序一致!

      由于CarPart继承Part;所以当CarPart初始化构造函数的时候的;先初始化Part成员变量再初始化CarPart成员变量;否则则会像开头那样提示的warning

  • 相关阅读:
    hdu 4107 Gangster 线段树(成段更新)
    hdu 3037 Saving Beans (lucas定理)
    hdu 3944 DP? (Lucas 定理)
    hdu 5038 Grade 水
    ASP.NET Core 配置 MVC
    ASP.NET Core 静态文件
    ASP.NET Core 异常和错误处理
    ASP.NET Core 中间件
    ASP.NET Core 项目配置 ( Startup )
    ASP.NET Core 基本项目目录结构
  • 原文地址:https://www.cnblogs.com/ztteng/p/3258543.html
Copyright © 2011-2022 走看看