| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 630 人关注过本帖
标题:这个类编译不报错 为什么执行有问题了
只看楼主 加入收藏
q236763612
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2008-10-22
收藏
 问题点数:0 回复次数:2 
这个类编译不报错 为什么执行有问题了
以下代码中我定义了一个Manager类,是继承的Employee类的。然后动态申请了一个Manager类的数组。可是在准备将数组成员按工资的多少从新排序时,编译没报错,执行却出了问题。不能输出排序结果,而且弹出C++ 2005 出错将关闭的窗口。
相关代码如下!





#include<string>
#include<iostream>
#include<cassert>
using namespace std;
class Employee
{protected:
    string name;
    int     ID;
public:
    Employee(char* a, int b)
        :name(a),ID(b)
    {}
    Employee (const Employee& s)
    {
        assert(this!=& s);
        name=s.name;
        ID=s.ID;}
Employee& operator=(const Employee& rhs)
{
    assert(this!=&rhs);
    name=rhs.name;
    ID=rhs.ID;
 return *this;
}
void  print()
{ cout<<name<<ID<<endl;}

};
class Manager:public  Employee
{
private:
    int sal;
public:
    Manager(char*a=" ", int b =0, int c =0)
        :Employee(a,b),
        sal(c)
    {}
Manager(const Manager& s)
:Employee(s)
{ sal=s.sal;}
Manager& operator=(const Manager& rhs)
{   ID=rhs.ID;
   name=rhs.name ;

    sal=rhs.sal;
    return *this;}
void print() {cout<<name<<'\t'<<ID<<"号"<<sal<<"圆"<<endl;}
void SetManager(){  cin>>name ; cin>>ID; cin>>sal;}
friend int showsal( const Manager& s)
{ return  s.sal ;}
friend  string  showname (const Manager& t)
{ return   t.name ;}

};
int main ()
{   double  p=0;  int sum=0;
     Manager t;
    Manager *s= new Manager[5];
  assert(s !=NULL);
  cout<<"please enter 5 elements"<<endl;
for(int i=0;i!=5;++i)
{s[i].SetManager ();}
cout<<"finished"<<endl;
for(int i=0;i!=5;++i)
{s[i].print();}
for (int i=0;i!=5;++i)
{
sum=sum+showsal(s[i]);}
  p=sum/5;
  cout<<"平均工资为:"<<p<< "圆"<<endl;
cout<<"工资比较高的人为:"<<endl;
for(int i=0;i!=5;++i)
{
    if( showsal(s[i])>=p)
        cout<<showname(s[i])<<endl;
}
for( int i=0;i!=5;++i)
for(int j=i+1;i!=5;++i)
if(showsal(s[i])>showsal(s[j]))  //这里是排序代码
  {t=s[i];
    s[i]=s[j];
    s[j]=t;}
for(int i=0;i!=5;++i) // 排序后从新输出数组成员
{s[i].print();}

delete []s;

}
搜索更多相关主题的帖子: 编译 
2008-11-08 13:45
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
收藏
得分:0 
单步调试~~

樱花大战,  有爱.
2008-11-08 16:34
shediao
Rank: 1
来 自:山东
等 级:新手上路
威 望:1
帖 子:52
专家分:0
注 册:2008-9-23
收藏
得分:0 
老兄,我帮你改了改,不知道是否是你想要的
你的排序的第二个for循环写错了,所以发生错误

// Employee.h: interface for the Employee class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_EMPLOYEE_H__040583E4_9F8E_4013_A78F_97567B739665__INCLUDED_)
#define AFX_EMPLOYEE_H__040583E4_9F8E_4013_A78F_97567B739665__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include<iostream>
#include<string>
using namespace std;
class Employee  
{
protected:
    string name;
    int Id;
public:
    Employee(){};
    Employee(string n,int i):name(n),Id(i){}
    Employee(Employee const &e){
        if(this!=&e){
            name=e.name;
            Id=e.Id;
        }
    }
    Employee& operator=(Employee const &e){
        name=e.name;
        Id=e.Id;
        return *this;
    }
    void print(){
        cout<<"name="<<name<<"  id="<<Id;
    }
    virtual ~Employee(){};

};


class Manager : public Employee  
{
private:
    int sal;
public:
    Manager(){};
    Manager(string n,int i=0,int j=0):Employee(n,i),sal(j){}
    Manager(Manager const &m):Employee(m){
        sal=m.sal;
    }
    Manager& operator=(Manager const &m){
        name=m.name;
        Id=m.Id;
        sal=m.sal;
        return *this;
    }    
    void print()
    {
        Employee::print();
        cout<<"  sal="<<sal<<endl;
    }
    void setManager(){
        cin>>name;
        cin>>Id;
        cin>>sal;
    }
    friend int showsal(Manager const &m){
        return m.sal;
    }
    friend string showname(Manager const &m){
        return m.name;
    }
    virtual ~Manager(){};

};

#endif // !defined(AFX_EMPLOYEE_H__040583E4_9F8E_4013_A78F_97567B739665__INCLUDED_)



// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Employee.h"
#include<cassert>
int main ()
{   
    double  p=0;
    int sum=0;
    int i;
    int j;
    Manager t;
    Manager *s= new Manager[5];
    assert(s !=NULL);
    cout<<"please enter 5 elements"<<endl;
    for(i=0;i<5;++i)
    {    
        s[i].setManager();
    }
    cout<<"finished"<<endl;
    for(i=0;i<5;++i)
    {
        s[i].print();
    }
    for (i=0;i<5;++i)
    {
        sum=sum+showsal(s[i]);
    }
    p=sum/5;
    cout<<"平均工资为:"<<p<< "圆"<<endl;
    cout<<"工资比较高的人为: ";
    for(i=0;i<5;++i)
    {
        if( showsal(s[i])>=p)
            cout<<showname(s[i])<<"   ";
    }
    cout<<endl;
    for(i=0;i<5;++i)
        for(j=i+1;j<5;++j)
            if(showsal(s[i])>showsal(s[j]))  //这里是排序代码
            {
                t=s[i];
                s[i]=s[j];
                s[j]=t;
            }
    for(i=0;i!=5;++i) // 排序后从新输出数组成员
    {
        s[i].print();
    }

    delete []s;
    return 0;
}

运行结果:
please enter 5 elements
shediao1 1 100
shediao2 2 123
shediao3 3 150
shediao4 4 108
shediao5 5 122
finished
name=shediao1  id=1  sal=100
name=shediao2  id=2  sal=123
name=shediao3  id=3  sal=150
name=shediao4  id=4  sal=108
name=shediao5  id=5  sal=122
平均工资为:120圆
工资比较高的人为:
shediao2
shediao3
shediao5
name=shediao1  id=1  sal=100
name=shediao4  id=4  sal=108
name=shediao5  id=5  sal=122
name=shediao2  id=2  sal=123
name=shediao3  id=3  sal=150
Press any key to continue
2008-11-08 22:55
快速回复:这个类编译不报错 为什么执行有问题了
数据加载中...
 
   



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

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