no matching function to call to “standard constructor”

前端 未结 4 1015
醉梦人生
醉梦人生 2021-01-20 04:27

I get a weird response from my c++ compiler. I searched the internet but came up with nothing usefull or helpfull...

Compiler Response:

4条回答
  •  伪装坚强ぢ
    2021-01-20 05:15

    First, the error. Your constructor for HexFloatingPoint is not initialising the base class, FloatingPoint. Since you have defined a constructor taking two arguments, and no default constructor (containing no arguments), derived classes must initialise it using the constructor you've defined:

    HexFloatingPoint::HexFloatingPoint(int sign_length,int exp_length) :
        FloatingPoint(sign_length, exp_length),  // ADD THIS
        significant_length(sign_length),
        exponent_length(exp_length)
    {}
    

    Alternatively, you might want to be able to leave the base-class members uninitialised, or initialise them to default values, in which case you'll need to provide a default constructor:

    FloatingPoint() {} // or initialise the members if you want
    

    but this probably isn't what you want to do; you probably want to initialise them properly.

    Secondly, the warning. Often when you have a polymorphic base class, you'll want to delete objects of the derived through a pointer to the base class. This is only allowed if the base class declares a virtual destructor; otherwise, such deletion gives undefined behaviour. I suggest you follow the compiler's advice and add one:

    virtual ~FloatingPoint() {}
    

    Finally, once you've fixed the compiler errors then (assuming there's no more code than what you posted) you'll get linker errors due to missing definitions of FloatingPoint::set_exponent and FloatingPoint::print. This is because you've declared them virtual but not pure, and haven't implemented them. They probably want to be pure virtual like set_significant:

    virtual void set_exponent(string number) = 0;
                                             ^^^
    

提交回复
热议问题