I haven’t written C++ in 80 years, but I encountered it today 类 "xxx" 不存在默认构造函数
and I forgot how to solve it...
customer.h
document:
#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.cpp
document:
#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.h
modify it in the file:
Customer(std::string name="", int age=-1);