| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1497 人关注过本帖
标题: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)
    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]]
搜索更多相关主题的帖子: new 成员 运行 
2008-08-13 19:09
沿途有鬼
Rank: 1
等 级:新手上路
帖 子:68
专家分:0
注 册:2008-7-20
收藏
得分:0 
xlh5225 非常感谢你的解答啊,你解答的这么详细我一点都不介意,要是论坛能多有几个你这样的热心人就好了,再次谢过~!

[[it] 本帖最后由 沿途有鬼 于 2008-8-14 11:01 编辑 [/it]]
2008-08-14 10:54
快速回复:C++中在用new分配类成员的类在运行阶段程序终止问题,请大家帮忙找找错 ...
数据加载中...
 
   



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

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