There is no corresponding function to call the "standard constructor"

I am getting a strange response from my C ++ compiler. I have searched the web but came up with nothing useful or helpful ...

Compiler response:

floating.hpp | line 29 | warning: "class HexFloatingPoint" has virtual functions, but no virtual destructor

In the constructor `HexFloatingPoint :: HexFloatingPoint (int, int) ':

float.cpp | line 5 | error: there is no suitable function to call `FloatingPoint :: FloatingPoint () '

floating.hpp | line 16 | note: candidates: FloatingPoint :: FloatingPoint (const FloatingPoint &)

floating.cpp | line 3 | note: FloatingPoint :: FloatingPoint (int, int)

These are the code files:

main.cpp

#include <iostream>
#include "floating.hpp"

using namespace std;

int main(int argc, char* argv[])
{

    return 0;
}

      

floating.hpp

#ifndef FLOATING
#define FLOATING

#include <string>
#include <vector>

using namespace std;

class FloatingPoint;
class HexFloatingPoint;
class HexDigit;
class HexDigitBool;
class HexDigitChar;

class FloatingPoint
{
private:
    int significant_length;
    int exponent_length;

public:
    FloatingPoint(int sign_length,int exp_length);
    virtual void set_significant(string number) = 0;
    virtual void set_exponent(string number);
    virtual void print();
};

class HexFloatingPoint : public FloatingPoint
{
private:
    vector<HexDigit*> significant_length;
    vector<HexDigit*> exponent_length;
public:
    HexFloatingPoint(int sign_length,int exp_length);
    void set_significant(string number);
    void set_exponent(string number);
    void print();
    ~HexFloatingPoint();
};

class HexDigit
{
public:
    virtual void print()=0;
    virtual void set_value(char c);
    virtual int get_value();
};

class HexDigitBool : public HexDigit
{
private:
    bool b[4];
public:
    void print();
    virtual void set_value(char c);
    virtual int get_value();
};

class HexDigitChar : public HexDigit
{
private:
    char c;
public:
    void print();
    virtual void set_value(char c);
    virtual int get_value();
};


#endif

      

floating.cpp

#include "floating.hpp"

FloatingPoint::FloatingPoint(int sign_length,int exp_length) : significant_length(sign_length),exponent_length(exp_length){}

HexFloatingPoint::HexFloatingPoint(int sign_length,int exp_length) : significant_length(sign_length),exponent_length(exp_length){}
void HexFloatingPoint::set_significant(string number){}
void HexFloatingPoint::set_exponent(string number){}
void HexFloatingPoint::print(){}
HexFloatingPoint::~HexFloatingPoint(){}

      

I hope you can help me. I've already tried adding FloatingPoint (); in floating .hpp and floating.cpp but it didn't help.

UPDATE 1

Constructor changed

HexFloatingPoint::HexFloatingPoint(int sign_length,int exp_length) : FloatingPoint(sign_length,exp_length){}

      

The compiler said no ...

floating.o||In function `_ZNSt12_Vector_baseIP8HexDigitSaIS1_EED2Ev':|
stl_vector.h:(.text+0x8)||undefined reference to `vtable for FloatingPoint'|
floating.o||In function `_ZN13FloatingPointC1Eii':|
floating.cpp|3|undefined reference to `vtable for FloatingPoint'|'

      

UPDATE 2

Change

class FloatingPoint
{
private:
    int significant_length;
    int exponent_length;

public:
    FloatingPoint(int sign_length,int exp_length);
    virtual void set_significant(string number) = 0;
    virtual void set_exponent(string number);
    virtual void print();
};

      

to

class FloatingPoint
{
private:
    int significant_length;
    int exponent_length;

public:
    FloatingPoint(int sign_length,int exp_length);
    virtual void set_significant(string number) = 0;
    virtual void set_exponent(string number) = 0;
    virtual void print() = 0;
};

      

resolved errors that occurred during update 1

changes

class HexFloatingPoint : public FloatingPoint
{
private:
    vector<HexDigit*> significant_length;
    vector<HexDigit*> exponent_length;
public:
    HexFloatingPoint(int sign_length,int exp_length);
    void set_significant(string number);
    void set_exponent(string number);
    void print();
    ~HexFloatingPoint();
};

      

to

class HexFloatingPoint : public FloatingPoint
{
private:
    vector<HexDigit*> significant_length;
    vector<HexDigit*> exponent_length;
public:
    HexFloatingPoint(int sign_length,int exp_length);
    void set_significant(string number);
    void set_exponent(string number);
    void print();
    virtual ~HexFloatingPoint();
};

      

warning

change

HexFloatingPoint::HexFloatingPoint(int sign_length,int exp_length) : significant_length(sign_length),exponent_length(exp_length){}

      

to

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

      

fixed the first problem

Thanks a lot guys!

+3


source to share


4 answers


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)

      

After providing any constructor for your class, you also need to explicitly provide a constructor that takes no parameters if your code calls the no argument constructor.

The warning clearly tells you that your code is calling the no-argument constructor when it calls the constructor for:

HexFloatingPoint(int, int)

      



So either:

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

    floating.hpp | line 29 | warning: "class HexFloatingPoint" has virtual functions, but no virtual destructor

This is an important warning. Your class HexFloatingPoint

has a member function virtual

which implies that it must provide a destructor as well virtual

.
Please note, if you do not provide a virtual destructor in the base class and call delete

the base class pointer pointing to the derived class object, then this will result in Undefined Behavior.

+1


source


HexFloatingPoint

inferred from FloatingPoint

, but you don't call any constructors FloatingPoint

on the initialization list in the constructor HexFloatingPoint

. This means the default constructor (no-parameter) FloatingPoint

is called, but the class doesn't define it, so you get an error.



Call a constructor FloatingPoint

on HexFloatingPoint

the initialization list, or provide a FloatingPoint

default constructor.

+1


source


floating.hpp | line 29 | warning: the HexFloatingPoint class has a virtual function, but not a virtual destructor

Add virtual destructor to HexFloatingPoint:

virtual ~HexFloatingPoint(); 

      

Once the class has virtual functions, you need to declare / define a virtual destructor, otherwise if you access the derived class using a base class pointer or reference in your client code, it may happen that the derived class's destructor is not called, the behavior is undefined. Usually, the derived part of the object is not removed (accessed via a 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: the HexFloatingPoint class has a virtual function, but not a virtual destructor

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

0


source


First, there is a mistake. Your constructor to HexFloatingPoint

not initialize the base class FloatingPoint

. Since you have defined a constructor with two arguments and a default (no arguments) constructor, derived classes must initialize it using the constructor you 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 may want to leave the base class members uninitialized or initialize them by default, in which case you need to provide a default constructor:

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

      

but that is probably not what you want to do; you probably want to initialize them correctly.

Second, a warning. Often times, when you have a polymorphic base class, you want to remove objects from the derived via a pointer to the base class. This is only permitted if the base class declares a virtual destructor; otherwise, such deletion gives undefined behavior. I suggest you follow the compiler guidelines and add one:

virtual ~FloatingPoint() {}

      

Finally, once you've fixed the compiler errors (assuming there is no more code there than you posted), you will get linker errors due to missing definitions FloatingPoint::set_exponent

and FloatingPoint::print

. This is because you declared them virtual, but not clean, and did not implement them. They probably want to be pure virtual, for example set_significant

:

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

      

0


source







All Articles