| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 769 人关注过本帖
标题:求助 指针对象数组 类间关系 问题 !
只看楼主 加入收藏
xlongjlu
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-5-13
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
求助 指针对象数组 类间关系 问题 !
//头文件 "thief.h"

#ifndef  THIEFH
#define  THIEFH

#include <iostream.h>

class Thief
{
public:
  Thief(float Ma) {  MoneyAmount=Ma;  }      
  void Display() {  cout<<MoneyAmount<<endl;  }   //输出金钱数目
  void SetMoneyAmount(float ma) {  MoneyAmount=ma;  }    //设置金钱数目
private:
  float MoneyAmount;   
};

#endif


//头文件 "police.h"


#ifndef  POLICEH
#define  POLICEH
#include <iostream.h>
#include "thief.h"
#include "policeoffice.h"

class Police
{
public:
 
  Police(float rew,char st[]): Reward(rew) {  strcpy(Name,st);  }      //构造函数
 
  void Arrest(Thief &);            //逮捕小偷
  float GetReward() {  return Reward;  }     //返回奖励
  char  Name[20];      // 警察名字
private:
  float Reward;     //奖励
};

void Police::Arrest(Thief &t)      //逮捕到小偷后警察奖金增加100  小偷金钱归零
{
  Reward=Reward+100;
  t.SetMoneyAmount(0.00);
}

#endif


//头文件 "policeoffice.h"


#ifndef  POLICEOFFICE
#define  POLICEOFFICE

#include <iostream.h>
#include "thief.h"
#include "police.h"

class PoliceOffice
{
public:
    PoliceOffice(int Rep,int Num): Reputation(Rep),Number(Num)    //构造函数  
    {
        for(int i=0;i<Number;i++)                                                            //动态创建Number个police 对象
            police[i] = NULL;
    }
        ~PoliceOffice()                                             //析构函数
        {
            for(int i=0;i<Number;i++)
            delete police[i];
            delete[] police;
        }
    void Initialize();                           //设置警察的信息
    Police*  GetPolice(char st[]);           //返回警察类指针
    void Display();                              //输出警察局信息
    void SetReputation();                 //设置声望   每逮捕一个小偷声望加1
private:
    int Reputation;                        
    int Number;
    Police* police[];                  
};



void PoliceOffice::Initialize()                //实现设置警察局中每个警察的信息
{
    police = new Police*[Number];

    for(int i=0;i<Number;i++)
    {
        char st[20];
        cout<<"请输入警察名字:"<<endl;
        cin>>st;
        police[i] = new Police(0.0,st);

        //Police(0.00,st);
    }
}

Police *  PoliceOffice::GetPolice(char st[])               //输入警察的name 返回指向该警察的指针
{
    for(int j=0;j<3;j++)
    {
        if(strcmp(st,police[j].Name)>0)
            return &police[i];
    }
}

void PoliceOffice::SetReputation()
{
    Reputation++;
}

void PoliceOffice::Display()           // 输出警察局声望  警察奖金
{
    cout<<"警察局的声望是:"<<Reputation<<endl;
    for(int i=0;i<Number;i++)
        cout<<"警察"<<police[i].Name<<"的奖金是:"<<police[i].GetReward()<<endl;
        
}

#endif

//主函数 "main.cpp"

#include <iostream.h>
#include <string.h>
#include "thief.h"
#include "police.h"
#include "policeoffice.h"

int main()
{
    Thief t1(500),t2(800),t3(300),t4(1000);        //创建四个Thief对象
    PoliceOffice S(100,3);                                   
    S.Initialize();
    Police *p=NULL;                               //创建指向Police 指针
    char *name[3]={"p1","p2","p3"};         
    p=S.GetPolice(name[0]);
    p->Arrest(t2);
    S.SetReputation();         
    p->Arrest(t3);
    S.SetReputation();
    p=S.GetPolice(name[1]);
    p->Arrest(t4);
    S.SetReputation();
    S.Display();
    return 0;
}

搜索更多相关主题的帖子: 关系 
2010-05-28 23:00
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:6 
回复 楼主 xlongjlu
请楼主把要问的问题描述清楚。

南国利剑
2010-05-30 00:50
xlongjlu
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-5-13
收藏
得分:0 
编译时出现如下错误:

1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(25): warning C4183: 'GetPolice': missing return type; assumed to be a member function returning 'int'
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(31): error C2143: syntax error : missing ';' before '*'
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(31): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(31): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(14): error C2065: 'police' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(14): error C2061: syntax error : identifier 'Police'
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(16): error C2065: 'police' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(21): error C2065: 'police' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(22): error C2065: 'police' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(22): error C2541: 'delete' : cannot delete objects that are not pointers
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(38): error C2065: 'police' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(38): error C2061: syntax error : identifier 'Police'
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(45): error C2065: 'police' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(45): error C2061: syntax error : identifier 'Police'
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(52): error C2143: syntax error : missing ';' before '*'
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(52): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(52): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(55): error C2065: 'police' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(55): error C2228: left of '.Name' must have class/struct/union
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(56): error C2065: 'police' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(69): error C2065: 'police' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(69): error C2228: left of '.Name' must have class/struct/union
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(69): error C2065: 'police' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\policeoffice.h(69): error C2228: left of '.GetReward' must have class/struct/union
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\main.cpp(13): error C2065: 'p' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\main.cpp(15): error C2065: 'p' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\main.cpp(16): error C2065: 'p' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\main.cpp(16): error C2227: left of '->Arrest' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\main.cpp(18): error C2065: 'p' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\main.cpp(18): error C2227: left of '->Arrest' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\main.cpp(20): error C2065: 'p' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\main.cpp(21): error C2065: 'p' : undeclared identifier
1>c:\users\xlong\documents\visual studio 2010\projects\exercise\exercise\main.cpp(21): error C2227: left of '->Arrest' must point to class/struct/union/generic type
1>          type is ''unknown-type''
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
2010-05-31 20:25
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:606
专家分:2499
注 册:2009-4-8
收藏
得分:6 
//头文件 "police.h"


#ifndef  POLICEH
#define  POLICEH
#include <iostream.h>
#include "thief.h"
#include "policeoffice.h"//不要包含它
////////////////////////////////////////
class PoliceOffice
{
public:
    PoliceOffice(int Rep,int Num): Reputation(Rep),Number(Num)    //构造函数  
    {
        for(int i=0;i<Number;i++)                                                            //动态创建Number个police 对象
            police[i] = NULL;
    }
        ~PoliceOffice()                                             //析构函数
        {
            for(int i=0;i<Number;i++)
            delete police[i];
            delete[] police;
        }
    void Initialize();                           //设置警察的信息
    Police*  GetPolice(char st[]);           //返回警察类指针
    void Display();                              //输出警察局信息
    void SetReputation();                 //设置声望   每逮捕一个小偷声望加1
private:
    int Reputation;                        
    int Number;
    Police* police[];   //   class PoliceOffice
{
public:
    PoliceOffice(int Rep,int Num): Reputation(Rep),Number(Num)    //构造函数  
    {
        for(int i=0;i<Number;i++)                                                            //动态创建Number个police 对象
            police[i] = NULL;
    }
        ~PoliceOffice()                                             //析构函数
        {
            for(int i=0;i<Number;i++)
            delete police[i];
            delete[] police;
        }
    void Initialize();                           //设置警察的信息
    Police*  GetPolice(char st[]);           //返回警察类指针
    void Display();                              //输出警察局信息
    void SetReputation();                 //设置声望   每逮捕一个小偷声望加1
private:
    int Reputation;                        
    int Number;
    Police* police[]; // Police** police;            
};
 还有几处错误是因为是指针不能用“.”改用->去访问成员。
我运行了,报错,估计是指针的问题,我有时间再看看,晕   
};

迭代的是人,递归的是神。
2010-06-01 21:58
迷失的木桶
Rank: 4
等 级:业余侠客
帖 子:52
专家分:230
注 册:2010-5-29
收藏
得分:6 
像这种问题不应该到论坛上求助,编译器都已经报告错误给你了,你却还不知道。真是无语。
2010-06-01 22:25
快速回复:求助 指针对象数组 类间关系 问题 !
数据加载中...
 
   



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

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