求解答,C++,error c2512

本文介绍了在C++编程过程中遇到的错误代码C2512,该错误通常与默认构造函数有关。通过示例代码展示了如何在类的定义和使用中引发此错误,并提供了相应的解决策略,包括正确使用拷贝构造函数和初始化列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<string>
using namespace std;
class CCourse{
protected:
	long no;
	float credit;
	static int total_course;
public:
	CCourse(long n,float c)
	{
		no=n;
		credit=c;
		total_course++;
	}
	CCourse(CCourse &a)
	{
		no=a.no;
		credit=a.credit;
	}
	~CCourse()
	{
		cout<<"调用CCourse析构函数"<<endl;
	}
	void print()
	{
		cout<<"课程号:"<<no<<endl;
		cout<<"课程学分:"<<credit<<endl;
	}
	int getTotalCourse();
	friend int getTotalNo(CCourse &);
	bool operator<(const CCourse &b);
};
class COOP:public CCourse{
protected:
	char *p_openby;
public:
	COOP(long n,float c,char *p):CCourse(n,c)
	{
		p_openby=new char[strlen(p)+1];
		strcpy(p_openby,p);
	}
	COOP(COOP &a)
	{
		no=a.no;
		credit=a.credit;
		p_openby=new char[strlen(a.p_openby)+1];
		strcpy(this->p_openby,a.p_openby);
	}
	bool select(const char *p_xh);
	void print();
	~COOP()
	{
		cout<<"调用COOP析构函数"<<endl;
		delete []p_openby;
	}
};
bool COOP::select(const char *p_xh)
{
	int r;
	char *a="2016";
	r=strncmp(p_xh,a,4);
		if(r==0)
			return true;
		else return false;
}
int CCourse::total_course=0;
int getTotalNo(CCourse &a)
{
	return a.no;
}
int CCourse::getTotalCourse()
{
	return total_course;
}
bool CCourse::operator <(const CCourse &b)
{
	if(this->credit<b.credit)
		return true;
	else return false;
}
void COOP::print()
{
	CCourse::print();
	cout<<p_openby<<endl;
}
int main()
{
	CCourse sub1(12,3.5);
	CCourse sub2(sub1);
	CCourse sub3(36,5);
	sub1.print();
	sub2.print();
	sub3.print();
	cout<<"课程总数:"<<sub3.getTotalCourse()<<endl;
	cout<<"课程号:"<<getTotalNo(sub3)<<endl;
	if(sub1<sub3)
		cout<<"sub1小于sub3"<<endl;
	else cout<<"sub1不小于sub3"<<endl;
	COOP sub4(2,5,"面向对象程序设计");
	COOP sub5(sub4);
	sub4.print();
	sub5.print();
	char *stu[10];
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值