C++中在用new分配类成员的类在运行阶段程序终止问题,请大家帮忙找找错误原因,谢谢!
//头文件#ifndef STOCK_H_H_
#define STOCK_H_H_
#include<iostream>
using std::ostream;
using std::istream;
class Stock
{
private:
char * str;
int shares;
int len;
double share_val;
double total_val;
void set_tot(){total_val=shares * share_val;}
public:
Stock();
Stock(const char * co,int n=0,double pr=0.0);
~Stock();
void buy(int num,double price);
void sell(int num,double price);
void update(double price);
Stock & operator=(const Stock & s);
Stock & operator=(const char * s);
const Stock & topval(const Stock & s)const;
friend ostream & operator<<(ostream & os,const Stock t);
};
#endif
//源文件
#include<iostream>
#include<cstring>
#include"stock.h"
//using std::ostream;
//using std::istream;
Stock::Stock()
{
len=4;
str=new char[1];
str[0]='\0';
shares=0;
share_val=0.0;
total_val=0.0;
}
Stock::Stock(const char * co,int n,double pr)
{
len=strlen(co);
str=new char[len+1];
strcpy(str,co);
shares=n;
share_val=pr;
set_tot();
}
Stock & Stock::operator=(const Stock & s)
{
if(this==&s)
return *this;
if(str!=0)
delete [] str;
else if(s.len>0)
{
len=s.len;
str=new char[len+1];}
else
{std::cerr<<"len < 0 ";
return *this;
}
if(str==0)
{
std::cerr<<"memor error!\n";
return *this;
}
strcpy(str,s.str);
shares=s.shares;
share_val=s.share_val;
set_tot();
return *this;
}
Stock & Stock::operator=(const char * s)
{
if(str!=0)
delete [] str;
len=strlen(s);
if(len>0)
str=new char[len+1];
else
return *this;
if(str==0)
return *this;
strcpy(str,s);
return *this;
}
Stock::~Stock()
{
delete [] str;
}
void Stock::buy(int num,double price)
{
if(num<0)
{
std::cerr<<"Number of shares purchased can't be negative. "
<<"Transaction is aborted.\n";
}
else
{
shares=shares+num;
share_val=price;
set_tot();
}
}
void Stock::sell(int num,double price)
{
using std::cerr;
if(num<0)
{
cerr<<"Number of shares purchased can't be negative. "
<<"Transaction is aborted.\n";
}
else if(num>shares)
{
cerr<<"You can't sell more than you have! "
<<"Transaction is aborted.\n";
}
else
{
shares=shares-num;
share_val=price;
set_tot();
}
}
void Stock::update(double price)
{
share_val=price;
set_tot();
}
const Stock & Stock::topval(const Stock & s)const
{
if(s.total_val>total_val)
return s;
else
return *this;
}
ostream & operator<<(ostream & os,const Stock t)
{
using std::endl;
os<<"Conpany: "<<t.str
<<" Shares: "<<t.shares<<endl
<<" Share Price: $"<<t.share_val
<<" Total Worth: $"<<t.total_val<<endl;
return os;
}
//main()
#include<iostream>
#include"stock.h"
const int STKS=4;
int main()
{
using std::cout;
using std::ios_base;
Stock stocks[STKS]=
{
Stock("NanoSmart",12,20.0),
Stock("Boffo objects",200,2.0),
Stock("Monolithic Objects",130,3.25),
Stock("Fleep Enterprises",60,6.5)
};
cout.precision(2);
cout.setf(ios_base::fixed,ios_base::floatfield);
cout.setf(ios_base::showpoint);
int st=0;
for(st=0;st<STKS;st++)
cout<<stocks[st];
Stock top(stocks[0]);
for (st=1;st<STKS;st++)
top=top.topval(stocks[st]);//死在这个地方
cout<<"\n Most valuable holding:\n";
cout<<top;//这里没显示进过比较价格最高的股票
return 0;//过了这里还有个终止报错
}
程序的运行结果是比较不出来最后的那个求出那个股票价格最高,但是算法应该没问题,在就是在显示完成之后出现终止运行问题,我想我关键是错在构造函数和operator=()深度赋值上,下面的关于判断内存申请是否成功之类的我也不知道写对了没有,麻烦各位帮我看看错在哪里,我自己看不出来了~
Stock & Stock::operator=(const Stock & s)
{
if(this==&s)
return *this;
if(str!=0)//这个 str 判断它是不是空的不知道写对了没有?
delete [] str;
else if(s.len>0)
{
len=s.len;
str=new char[len+1];}
else
{std::cerr<<"len < 0 ";
return *this;//如果len<0应该返回什么呢?
}
if(str==0)//申请完内存,判断是否申请成功不知道写得是不是对的?
{
std::cerr<<"memor error!\n";
return *this;//内存申请什么应该返回什么呢?
}
strcpy(str,s.str);
shares=s.shares;
share_val=s.share_val;
set_tot();
return *this;
}
下面是我创好的VC工程,大家直接下载点击123Stock.dsw帮我看看错误,感激不尽啊~
123Stock.rar
(867.09 KB)
[[it] 本帖最后由 沿途有鬼 于 2008-8-13 21:33 编辑 [/it]]