新人求教大神assert引发的一系列错误

最近为了考研复试重新学习c++,开发工具用的是vs2015,在学习类模板时实验Array类模板时一直出现Assertion failed,如下为代码,报错为Assertion failed:i >= 0 && i < size,即下列程序63行,已作出标记,在一个求质数函数类模板中引用该类模板,数组size设置为30时没有报错,而设置为100会出现Assertion failed 

#ifndef _ARRAY_H_
#define _ARRAY_H_
#include<cassert>
template<class T>
class Array {
private:
	T* list;
	int size;
public:
	Array(int sz = 50);
	Array(const Array<T>&a);
	~Array();
	Array<T>& operator=(const Array<T>& rhs);
	T& operator[](int i);
	const T& operator[](int i)const;
	operator T* ();
	operator const T*()const;
	int getSize()const;
	void resize(int sz);
};


template<class T>
Array<T>::Array(int sz = 50) {
	assert(sz >= 0);
	size = sz;
	list = new T[size];
}


template<class T>
Array<T>::Array(const Array<T>&a) {
	size = a.size;
	list = new T[size];
	for (int i = 0;i < size;i++)
		list[i] = a.list[i];
}


template<class T>
Array<T>::~Array() {
	delete[]list;
}


template<class T>
Array<T>& Array<T>::operator=(const Array<T>& rhs) {
	if (&rhs != this) {
		if (size != rhs.size) {
			delete[]list;
			size = rhs.size;
			list = new T[size];
		}
		for (int i = 0;i < size;i++)
			list[i] = rhs.list[i];
	}
	return *this;
}


template<class T>
T& Array<T>::operator[](int i) {
	assert(i >= 0 && i < size);                      //此处assertion报错
	return list[i];
}


template<class T>
const T& Array<T>::operator[](int i)const {
	assert(i >= 0 && i < size)
		return list[i];
}


//重载指针转换运算符,将Array类的对象名转换为T类型指针,指向当前对象中的私有数据
//因而可以像使用普通数组首地址一样使用Array类的对象名
template<class T>
Array<T>::operator T* () {
	return list;
}


template<class T>
Array<T>::operator const T*()const {
	return list;
}


template<class T>
int Array<T>::getSize()const {
	return size;
}


template<class T>
void Array<T>::resize(int sz) {
	assert(sz >= 0);
	if (sz = size)
		return;
	T* newList = new T[sz];
	int n = (sz < size) ? sz : size;
	for (int i = 0;i < n;i++)
		newList[i] = list[i];
	delete[]list;
	list = newList;
	size = sz;
}
#endif

此外,在一个个人银行账户管理程序中引用了该类模板,整个程序共有7个文件,date.h,date.cpp为日期类的头文件和实现,account.h和accout.cpp为帐户类的头文件和实现,accumulator.h为辅助account中计算利率的辅助类,主函数运行时依次进行 a    s    S3755217    0.015操作后,报错,Assertion failed,同样上面第58行,注释掉array.h中assert这一行后,上方操作之后不报错了,继续进行依次输入 a   s   02342342    0.015出现CRT detected that the application wrote to memory after end of heap buffer,求大佬指点

date.h

#ifndef _DATE_H_
#define _DATE_H_
class Date {
public:
	Date(int year, int month, int day);
	int getYear()const { return year; }
	int getMonth()const { return month; }
	int getDay()const { return day; }
	int getMaxDay()const;                       //获得当月多少天
	bool isLeapYear()const {                    //判断是否闰年
		return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
	}
	void show()const;
	int operator-(const Date &date)const {      //把原来的distance函数改为重载运算符,更加直观
		return totalDays - date.totalDays;
	}
private:
	int year, month, day;
	int totalDays;
};
#endif

date.cpp

#include"date.h"
#include<iostream>
#include<cstdlib>
using namespace std;

namespace {
	const int DAYS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
}

Date::Date(int year, int month, int day) :year(year), month(month), day(day) {
	if (day <= 0 || day > getMaxDay()) {
		cout << "无效日期!";
		show();
		cout << endl;
		exit(1);
	}
	int years = year - 1;
	totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day;
	if (isLeapYear() && month > 2)totalDays++;
}

int Date::getMaxDay()const {
	if (isLeapYear() && month == 2)
		return 29;
	else
		return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
}

void Date::show()const {
	cout << getYear() << "-" << getMonth() << "-" << getDay();
}

accumulator.h

#ifndef _ACCUMULATOR_H_
#define _ACCUMULATOR_H_
#include"date.h"
class Accumulator {
public:
	Accumulator(const Date &date, double value) :lastDate(date), value(value), sum(0) {}
	//获得日期date的累加结果
	double getSum(const Date &date)const {
		return sum + value*(date - lastDate);
	}
	//在date将数值变更为value
	void Change(const Date &date, double value) {
		sum = getSum(date);
		lastDate = date;
		this->value = value;
	}
	//初始化,将日期变为date,数值变为value,累加器清零
	void Reset(const Date &date, double value) {
		lastDate = date;
		this->value = value;
		sum = 0;
	}
private:
	Date lastDate;     //上次变更数值的日期
	double value;     //当前值
	double sum;       //数值按日累加之和
};
#endif

account.h

#ifndef _ACCOUNT_H_
#define _ACCOUNT_H_
#include"date.h"
#include"accumulator.h"
#include<string>
//帐户基类
class Account {
protected:
	//供派生类调用的构造函数,id为帐户
	Account(const Date &date, const std::string &id);
	//记录一笔账,date为日期,amount为金额,desc为说明
	void Record(const Date &date, double amount, const std::string &desc);
	//报告错误信息
	void Error(const std::string &msg)const;
public:
	const std::string &getId()const { return id; }
	double getBalance()const { return balance; }
	static double getTotal() { return total; }
	//Account中的纯虚函数
	virtual void Deposit(const Date &date, double amount, const std::string &desc) = 0;
	virtual void Withdraw(const Date &date, double amount, const std::string &desc) = 0;
	virtual void settle(const Date &date) = 0;
	virtual void show()const;
private:
	std::string id;//帐号
	double balance;//余额
	static double total;//所有账户总额
};

//储蓄账户
class SavingsAccount :public Account {
public:
	//构造函数
	SavingsAccount(const Date &date, const std::string &id, double rate);
	double getRate()const { return rate; }
	//存入现金
	void Deposit(const Date &date, double amount, const std::string &desc);
	//取出现金
	void Withdraw(const Date &date, double amount, const std::string &desc);
	void settle(const Date &date);
private:
	Accumulator acc;//辅助计算的累加器
	double rate;//存款年利率
};

//信用账户
class CreditAccount :public Account {
public:
	//构造函数
	CreditAccount(const Date &date, const std::string &id, double credit, double rate, double fee);
	double getCredit()const { return credit; }
	double getRate()const { return rate; }
	double getFee()const { return fee; }
	double getAvailableCredit()const {
		if (getBalance() < 0)
			return credit + getBalance();
		else
			return credit;
	}
	//存入现金
	void Deposit(const Date &date, double amount, const std::string &desc);
	//取出现金
	void Withdraw(const Date &date, double amount, const std::string &desc);
	//结算利息和年费,每月1日调用一次
	void settle(const Date &date);
	void show()const;
private:
	Accumulator acc;//辅助计算的累加器
	double credit;//信用额度
	double rate;//欠款的日利率
	double fee;//信用卡年费
	double getDebt()const {
		double balance = getBalance();
		return (balance < 0 ? balance : 0);
	}

};
#endif

account.cpp

#include"account.h"
#include<cmath>
#include<iostream>
using namespace std;

double Account::total = 0;

//Account类的实现
Account::Account(const Date &date, const std::string &id) :id(id), balance(0) {
	date.show();
	cout << "\t#" << id << "已经创建。" << endl;
}

void Account::Record(const Date &date, double amount, const std::string &desc) {
	amount = floor(amount * 100 + 0.5) / 100;
	balance += amount;
	total += amount;
	date.show();
	cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
}

void Account::Error(const std::string &msg)const {
	cout << "错误!(#" << id << "):" << msg << endl;
}

void Account::show()const {
	cout << id << "\t余额:" << balance;
}

//SavingsAccount类的实现
SavingsAccount::SavingsAccount(const Date &date, const std::string &id, double rate) :Account(date, id), rate(rate), acc(date, 0) {}

void SavingsAccount::Deposit(const Date &date, double amount, const std::string &desc) {
	Record(date, amount, desc);
	acc.Change(date, getBalance());
}

void SavingsAccount::Withdraw(const Date &date, double amount, const std::string &desc) {
	if (amount > getBalance()) {
		Error("余额不足!");
	}
	else {
		Record(date, -amount, desc);
		acc.Change(date, getBalance());
	}
}

void SavingsAccount::settle(const Date &date) {
	if (date.getMonth() == 1) {
		double interest = acc.getSum(date)*rate / (date - Date(date.getYear() - 1, 1, 1));
		if (interest != 0)
			Record(date, interest, "利息");
		acc.Reset(date, getBalance());
	}
}

//CreditAccount类的实现
CreditAccount::CreditAccount(const Date &date, const std::string &id, double credit, double rate, double fee)
	:Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) {}

void CreditAccount::Deposit(const Date &date, double amount, const std::string &desc) {
	Record(date, amount, desc);
	acc.Change(date, getDebt());
}

void CreditAccount::Withdraw(const Date &date, double amount, const std::string &desc) {
	if (amount - getBalance() > credit) {
		Error("信用额不足!");
	}
	else {
		Record(date, -amount, desc);
		acc.Change(date, getDebt());
	}
}

void CreditAccount::settle(const Date &date) {
	double interest = acc.getSum(date)*rate;
	if (interest != 0)
		Record(date, interest, "利息");
	if (date.getMonth() == 1)
		Record(date, -fee, "年费");
	acc.Reset(date, getDebt());
}

void CreditAccount::show()const {
	Account::show();
	cout << "\t#可用信用额度:" << getAvailableCredit();
}
主函数
#include"account.h"
#include"Array.h"
#include<iostream>
using namespace std;

int main() {
	Date date(2008, 11, 1);
	Array<Account *>accounts(0);//创建帐户数组,0个元素
	cout << "操作指令:(a)增加一个账户 (d)存款 (w)取款 (s)显示帐户信息 (c)改变日期 (n)改变月份(变为下一个月) (e)退出" << endl;
	char cmd;
	do {
		date.show();
		cout << "\t总金额为:" << Account::getTotal() << "\t输入指令>";
		char type;
		int index, day;
		double amount, credit, rate, fee;
		string id, desc;
		Account* account;
		cin >> cmd;
		switch (cmd) {
		case'a':
			cout << "您选择了增加一个帐户,请输入帐户类型:(s)储蓄账户  (c)信用账户>";
			cin >> type;
			cout << endl;
			cout << "请输入帐户的ID>";
			cin >> id;
			cout << endl;
			if (type == 's') {
				cout << "您选择了储蓄账户,请输入该账户的年利率>";
				cin >> rate;
				cout << endl;
				account = new SavingsAccount(date, id, rate);
			}
			else {
				cout << "您选择了信用账户,请依次输入该账户的信用额度、日利率、年费>";
				cin >> credit >> rate >> fee;
				cout << endl;
				account = new CreditAccount(date, id, credit, rate, fee);
			}
			accounts.resize(accounts.getSize() + 1);
			accounts[accounts.getSize() - 1] = account;
			break;
		case'd':
			cout << "您选择了存款,请依次输入存款账户的下标和存款金额>";
			cin >> index >> amount;
			cout << endl;
			cout << "请输入存款说明>";
			getline(cin, desc);
			cout << endl;
			accounts[index]->Deposit(date, amount, desc);
			break;
		case'w':
			cout << "您选择了取款,请依次输入取款账户的下标和取款金额>";
			cin >> index >> amount;
			cout << endl;
			cout << "请输入取款说明>";
			getline(cin, desc);
			cout << endl;
			accounts[index]->Withdraw(date, amount, desc);
			break;
		case's':
			cout << "您选择了显示所有账户:" << endl;
			for (int i = 0;i < accounts.getSize();i++) {
				cout << "[" << i << "]";
				accounts[i]->show();
				cout << endl;
			}
			break;
		case'c':
			cout << "您选择了改变日期,请输入日期>";
			cin >> day;
			cout << endl;
			if (day < date.getDay())
				cout << "不可更改为以往日期!";
			else if (day>date.getMaxDay())
				cout << "无效的日期!";
			else
				date = Date(date.getYear(), date.getMonth(), day);
			break;
		case'n':
			cout << "您选择了下一个月." << endl;
			if (date.getMonth() == 12)
				date = Date(date.getYear() + 1, 1, 1);
			else
				date = Date(date.getYear(), date.getMonth() + 1, 1);
			for (int i = 0;i < accounts.getSize();i++)
				accounts[i]->settle(date);
			break;
		}
	} while (cmd != 'e');
	for (int i = 0;i < accounts.getSize();i++)
		delete accounts[i];
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值