构造函数的调用时机,以及没有合适的默认构造的报错问题

默认情况下:
编译器会给一个类至少提供三个函数:

  • 默认构造函数
  • 拷贝构造函数
  • 析构函数

当用户定义了有参构造函数,c++就不再提供默认无参构造,但会提供默认拷贝构造
当用户定义了拷贝构造函数,c++就不再提供其他构造函数

当编译器不再提供构造函数时,用户就不能再调用相应的构造函数;而出现没有合适的默认构造函数这样的报错时,说明用户在编译器没有提供默认构造函数时调用了默认构造函数,故会报错

解决方法是:手动添上一个没有函数体的默认构造函数

例1

#include<iostream>
using namespace std;

class Person
{
public:
	Person(string name, int age)
	{
		this->Name = name;
		this->Age = age;
	}
	string Name;
	int Age;
};

int main()
{
	//Person p;  **报错:不存在默认构造函数**
	Person("张三", 25);	
}

例2、该案例是黑马程序员c++视频中的案例

.hpp
#pragma once
#include<iostream>
using namespace std;

template<class T>
class MyArray
{
public:
	MyArray(int capacity)
	{
		this->Capacity = capacity;
		this->size = 0;
		this->myArray = new T[this->Capacity];
	}

	//当有数据在堆区开辟时,自己写拷贝构造函数,防止浅拷贝带来的问题
	MyArray(const MyArray& arr)
	{
		this->Capacity = arr.Capacity;
		this->size = arr.size;
		//this->myArray = arr.myArray;//浅拷贝
		this->myArray = new T[arr.Capacity];//深拷贝

		//将arr中原有的数据拷贝过来
		for (int i = 0; i < arr.size; i++)
		{
			this->myArray[i] = arr.myArray[i];
		}
	}

	//operator= 防止浅拷贝问题
	MyArray& operator=(const MyArray& arr)
	{
		cout << "operator调用" << endl;
		//先判断原来堆区是否有数据,如果有先释放
		if (this->myArray != NULL)
		{
			delete[] this->myArray;
			this->myArray = NULL;
			this->Capacity = 0;
			this->size = 0;
		}

		//深拷贝
		this->Capacity = arr.Capacity;
		this->size = arr.size;
		this->myArray = new T[arr.Capacity];
		for (int i = 0; i < arr.size; i++)
		{
			this->myArray[i] = arr.myArray[i];
		}
		return *this;
	}

	//尾插法
	void push_back(const T& val)
	{
		//判断容量是否等于大小
		if (this->Capacity == this->size)
		{
			return;
		}
		this->myArray[this->size] = val;
		this->size++;
	}

	//尾删法
	void pop_back()
	{
		if (this->size == 0)
		{
			return;
		}
		this->size--;
	}

	//返回数组容量
	int getCapacity()
	{
		return this->Capacity;
	}

	//返回数组大小
	int getSize()
	{
		return this->size;
	}

	//通过下标方式访问数组中的元素
	T& operator [](int index)
	{
		return this->myArray[index];
	}

	~MyArray()
	{
		if (this->myArray != NULL)
		{
			delete[] this->myArray;
			this->myArray = NULL;
		}
	}
private:
	T* myArray;
	int Capacity;
	int size;
};

.cpp

class Person
{
public:
	Person(string name,int age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}
	string m_Name;
	int m_Age;
};

void test2()
{
	MyArray<Person> arr(10);
	Person p1("a", 1);
	Person p2("b", 2);
	Person p3("c", 3);
	Person p4("d", 4);
	arr.push_back(p1);
	arr.push_back(p2);
	arr.push_back(p3);
	arr.push_back(p4);
	cout << "自定义数据类型:" << endl;
	for (int i = 0; i < arr.getSize(); i++)
	{
		cout << arr[i].m_Name << " " << arr[i].m_Age << endl;
	}
	cout << arr.getCapacity() << endl;
	cout << arr.getSize() << endl;
}

int main()
{
	test2();
}

运行会报错
在这里插入图片描述
在这里插入图片描述出错的原因就在于红框这句,尖括号内当告诉模板Person类型的数据时,小括号内表示arr中有十个person类型的数据,此时会十次调用person类的默认无参构造函数,因为当用户提供有参构造函数时,编译器就不再提供默认无参构造函数,没有默认无参构造函数而又调用了默认无参构造函数,故会报错
解决方法就是在person类中手动添加person的默认无参构造,即Person(){};

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值