标题:C++中在用new分配类成员的类在运行阶段程序终止问题,请大家帮忙找找错误原因 ...
取消只看楼主
沿途有鬼
Rank: 1
等 级:新手上路
帖 子:68
专家分:0
注 册:2008-7-20
 问题点数:0 回复次数:1 
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)//这个 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;
}   
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;//过了这里还有个终止报错
}
下面是我创好的VC工程,大家直接下载点击123Stock.dsw帮我看看错误,感激不尽啊~
123Stock.rar (867.1 KB)
搜索更多相关主题的帖子: new 成员 运行 
2008-08-13 21:40
沿途有鬼
Rank: 1
等 级:新手上路
帖 子:68
专家分:0
注 册:2008-7-20
得分:0 
没人知道吗?大家帮帮忙吧,都等了2天了实在是不会啊~
2008-08-15 09:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-228185-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.709165 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved