BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #29544同步于 2009/10/9
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

类中成员函数入口参数为类,程序总是输入值后出错

kkxhx
2009/10/9镜像同步3 回复
#include<iostream> #include<cstdlib> using namespace std; class List { public: List(int length) ; //构造函数 * void InputElement(int length) ; //向线性表中写入数据的函数 * int GetMaxLength() {return MaxLength ;} //得到线性表的容量 * void OutputList() ; //打印线性表函数 * ~List() {delete []elements ;} //析构函数 * private: int *elements ; //指向线性表的指针 int MaxLength ; //线性表的容量 int CurLength ; //线性表当前的元素个数 }; List ::List(int length) { MaxLength = length ; elements = new int[MaxLength] ; CurLength = 0 ; for(int i = 0 ; i < MaxLength ; i++) elements[i] = 0 ; } void List::InputElement(int length) { for(int i = 0 ; i < length ; i++) { cin >> elements[i] ; CurLength ++ ; } } void List::OutputList() { for(int i = 0 ; i < CurLength ; i++) cout << elements[i] << " " ; cout << endl ; } class UniteList { public: UniteList(List nl1 , List nl2) ; void OutputUniteList() ; ~UniteList() {delete []L ;} private: int *L ; int LMaxLength ; int LCurLength ; List L1 , L2 ; }; UniteList::UniteList(List nl1 , List nl2) : L1(nl1) , L2(nl2) { LMaxLength = L1.GetMaxLength() + L2.GetMaxLength() ; LCurLength = 0 ; L = new int[LMaxLength] ; for(int i = 0 ; i < LMaxLength ; i ++) L[i] = 0 ; } void UniteList::OutputUniteList() { for(int i = 0 ; i < LCurLength ; i ++) cout << L[i] << " " ; cout << endl ; } int main() { int length , number ; //供函数7使用 cout << "Please input the length of the list1:" ; cin >> length ; List list1(length) ; cout << "Please input the number of value you want to putin:" ; cin >> number ; cout << "Please input the values of the list1:" ; list1.InputElement(number) ; cout << "Please input the length of the list2:" ; cin >> length ; List list2(length) ; cout << "Please input the number of value you want to putin:" ; cin >> number ; cout << "Please input the values of the list2:" ; list2.InputElement(number) ; UniteList ul(list1 , list2) ; ul.OutputUniteList() ; return 0; }
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
Jarod机器人#1 · 2009/10/9
执行到哪里错了。。。。。。。。。。。。。。。。
kkxhx机器人#2 · 2009/10/9
【 在 Jarod 的大作中提到: 】 : 执行到哪里错了。。。。。。。。。。。。。。。。 输入6个数据后,点击回车运行,弹出带有debug assertion failed……字样的对话框提示出错,不知是哪里出错了~~
Jarod机器人#3 · 2009/10/9
如果你第一个数输入1,。。。。。。; 如果你第一个数输入2,。。。。。。; 我怎么知道你的第6个输入是哪一行代码? 你单步调试一下吧。。。用F11 【 在 kkxhx 的大作中提到: 】 : 输入6个数据后,点击回车运行,弹出带有debug assertion failed……字样的对话框提示出错,不知是哪里出错了~~