no matching function to call to “standard constructor”

前端 未结 4 1016
醉梦人生
醉梦人生 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:13

    floating.hpp|line 29|warning: `class HexFloatingPoint' has virtual functions but non-virtual destructor

    Add a virtual destructor to the HexFloatingPoint:

    virtual ~HexFloatingPoint(); 
    

    As soon as a class has virtual functions, you need to declare/define a virtual destructor, otherwise if you access the derived class through a base class pointer or a reference in your client code, it may happen that the destructor of the derived class doesn't get called, the behaviour is undefined. Usually, the derived part of the object is not deleted (access via base reference or pointer) in which case, you are left with a memory leak. Check out Effective C++, item 7.

    floating.hpp|line 29|warning: `class HexFloatingPoint' has virtual functions but non-virtual destructor

    As soon as you define a constructor for a class, the default constructor HexFloatingPoint() is not defined automatically, you need to explicitly add it to the class declaration/definition.

    0 讨论(0)
  • 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;
                                             ^^^
    
    0 讨论(0)
  • 2021-01-20 05:18
    In constructor `HexFloatingPoint::HexFloatingPoint(int, int)':
    
    floating.cpp|line 5|error: no matching function for call to `FloatingPoint::FloatingPoint()'
    
    floating.hpp|line 16|note: candidates are: FloatingPoint::FloatingPoint(const FloatingPoint&)
    
    floating.cpp|line 3|note: FloatingPoint::FloatingPoint(int, int)
    

    Once you provide any constructor for your class you also have to explicitly provide a constructor which does not take any parameters if your code invokes the no argument constructor.

    The warning clearly tells you that your code invokes a no argument constructor when it calls the constructor for:

    HexFloatingPoint(int, int)
    

    So either:

    • You should provide a no argument constructor explicitly or
    • You should use member initializer list and invoke the desired version of constructor from base class if you don't want to provide a default no argument constructor for the base class.

      floating.hpp|line 29|warning: `class HexFloatingPoint' has virtual functions but non-virtual destructor

    This is an important warning. Your class HexFloatingPoint has an virtual member function which implies that it should also provide an virtual destructor.
    Note that if you do not provide a virtual destructor in your base class and you call delete on a base class pointer pointing to a derived class object then it will result in Undefined Behavior.

    0 讨论(0)
  • 2021-01-20 05:20

    HexFloatingPoint is derived from FloatingPoint, but you don't call any of FloatingPoint's constructors in the initialiser list in HexFloatingPoint's constructor. This means the default (no-parameter) constructor of FloatingPoint gets called, but the class doesn't define it, so you get the error.

    Either call a constructor of FloatingPoint in HexFloatingPoint's initialiser list, or give FloatingPoint a default constructor.

    0 讨论(0)
提交回复
热议问题