[Daily News] C++ class "xxx" does not have a default constructor

I haven’t written C++ in 80 years, but I encountered it today 类 "xxx" 不存在默认构造函数and I forgot how to solve it...

customer.hdocument:

#ifndef CUSTOM_H
#define CUSTOM_H

#include<string>

class Customer{
    
    
    public:
        Customer(std::string name, int age);
        // virtual ~Customer();
        void printInfo() const;

    private:
        std::string m_strName;
        int m_iAge;
};

#endif

customer.cppdocument:

#include <iostream>
#include "customer.h"

using namespace std;

Customer::Customer(string name, int age){
    
    
    m_strName = name;
    m_iAge = age;
}

void Customer::printInfo() const{
    
    
    cout << "name: " << m_strName << endl;
    cout << "age : " << m_iAge << endl;
    cout << endl;
}

Code error location

m_xxxx = new Customer[12]; // 堆申请内存(可能会失败)

Error:

"Customer" 不存在默认构造函数

Just pass it a default value and customer.hmodify it in the file:

Customer(std::string name="", int age=-1);

Guess you like

Origin blog.csdn.net/HaoZiHuang/article/details/131881441