C ++ No suitable default constructor

I get the error "A: No suitable default constructor". The line number that points me to is the line where I am initializing class B.

I tried to do the init list as suggested in similar posts, but that didn't fix the error.

There are many other functions in the program that I cut out for readability and simplicity, since they are not related to the error.

I create object B, which in the constructor creates two objects of class A, each of which receives a pointer to a separate function.

// Class A
template <typename T> class A   
{
public:

// constructor
A(int c, bool(*Ptr)(T, T));
};


// Class B
template <typename T> class B
{
public:
//constructor
B(int c);

A <T> oneHolder;   // Class A objects
A <T> twoHolder;

bool(*lowPtr)(T, T) = &lowerThan;           // pointer to a function
bool(*highPtr)(T, T) = &higherThan;         // pointer to a function
};

//Class A constructor
template <typename T>
A<T>::A(int c, bool(*fPtr)(T, T)) {

    m_Size = c;
    // set function pointer
    func_ptr = fPtr;

}


//B Constructor
template <typename T>
B<T>::B(int c){ // Might need initialization list, error points here

    m_Size = c;

    // Create two A objects
    A<T> one(c, lowPtr); // passing function pointer
    A<T> two(c, highPtr);


    lowHolder = one;
    highHolder = two;

}

      

+3


source to share


1 answer


You must call the constructor A from the constructor B. Class A does not have a default constructor.

If you change the constructor to the following it will work



A(int c = 0, bool(*Ptr)(T, T) = nullptr);

      

+3


source







All Articles