散分!!!!提问。。。。。。。。。。。

cainiao0823 2004-10-17 01:26:09

#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <string>

using namespace std;

class Consumer
{
public:
Consumer ()
:_flag (true)
{
cout<<"consumer"<<endl;
}
void SetFlag (bool flag) { _flag = flag;}
bool GetFlag () {return _flag;}
private:
bool _flag;
};

class Goods
{
public:
Goods (string name, double)
:_name (name),_price (price) {}
string Name () {return _name;}
double Price () {return _price;}
private:
string _name;
double _price;
};
Goods merchandise [5] = {Goods("Apple", 2.0),Goods("orange",1.8),Goods("pig",16.2),
Goods("Cake",6.4),Goods"pear",1.4};


class Shopping
{
public:
Shopping ()
{}
Consumer * GetConsumer () {return _consumer;}
private:
Consumer _consumer [5];

};

class Process
{
public:
Process (Shopping shopping)
: _shopping (shopping)
{}
~Process ();
int Run ( );
private:
Shopping _shopping;

}

int Process::Run ( )
{
while (getch() != 'Q')
{
cout<<"Wellcome our client!"<<endl;
for (int i = 0; i<5; ++i)
{
int j = rand ()*5;
if (_shopping.GetConsumer()[j].GetFlag() != false)
{
cout<<"You are our "<< i <<" client"
<<"Please come in"<<endl;
cout<<"OK,take your time."<<endl;
int kig = rand()*5;
int num = rand()*5;
cout<<"the "<<j<<" client"
<<"have bought" << kig
<<"Kilogram"<<merchandise[num].Name()
<<endl;
cout<<"OK,lets go to the counts!";
cout<<"Your should pay for "
<<kig * merchandise[i].Price ()
<<endl;
cout<<"Wellcome to come again next time!";
_shopping.GetConsumer()[j].SetFlag(false);
}
}
}
return 0;
}

int main()
{

Shopping myClient;
Process process (myClient);
process::Run ();

return 0;
}


...全文
82 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cainiao0823 2004-10-18
  • 打赏
  • 举报
回复
谢谢大家

找到错误了,结贴
cainiao0823 2004-10-17
  • 打赏
  • 举报
回复

to::o1n(小毛子)
把Goods merchandise [5] = {Goods("Apple", 2.0),Goods("orange",1.8),Goods("pig",16.2),
Goods("Cake",6.4),Goods("pear",1.4)};的定义放在int Process::Run ()前面
刚才我打错了
cainiao0823 2004-10-17
  • 打赏
  • 举报
回复


我用debug跟踪是错误提符显示在Consummer 类的 bool GetFlag () {return _flag;}
private:
bool _flag;

附近,但我看不出错在那里

xuzheng318 2004-10-17
  • 打赏
  • 举报
回复
有可能数组越界,或者内存泄漏,溢出 什么的,编译是看不出来得!
要debug跟踪!
diaoni 2004-10-17
  • 打赏
  • 举报
回复
int j = rand ()*5; //j=?????
if (_shopping.GetConsumer()[j].GetFlag() != false) //_shopping.GetConsumer()[j]数组越界

好象下面也有这样的错误。
o1n 2004-10-17
  • 打赏
  • 举报
回复

#include <iostream>

#include <string>

using namespace std;
#include <stdlib.h>
#include <conio.h>

class Consumer
{
public:
Consumer ()
:_flag (true)
{
cout<<"consumer"<<endl;
}
void SetFlag (bool flag) { _flag = flag;}
bool GetFlag () {return _flag;}
private:
bool _flag;
};

class Goods
{
public:
Goods (string name, double price):_name (name),_price (price) {}//error
string Name () {return _name;}
double Price () {return _price;}
private:
string _name;
double _price;
};
Goods merchandise [5] = {Goods("Apple", 2.0),Goods("orange",1.8),Goods("pig",16.2),
Goods("Cake",6.4),Goods("pear",1.4)};//error


class Shopping
{
public:
Shopping ()
{}
Consumer * GetConsumer () {return _consumer;}
private:
Consumer _consumer [5];

};

class Process
{
public:
Process (Shopping shopping): _shopping (shopping)
{}

int Run ( );
private:
Shopping _shopping;

};//error

int Process::Run ()
{
while (getch() != 'Q')
{
cout<<"Wellcome our client!"<<endl;
for (int i = 0; i<5; ++i)
{
int j = rand ()*5;
if (_shopping.GetConsumer()[j].GetFlag() != false)
{
cout<<"You are our "<< i <<" client"
<<"Please come in"<<endl;
cout<<"OK,take your time."<<endl;
int kig = rand()*5;
int num = rand()*5;
cout<<"the "<<j<<" client"
<<"have bought" << kig
<<"Kilogram"<<merchandise[num].Name()
<<endl;
cout<<"OK,lets go to the counts!";
cout<<"Your should pay for "
<<kig * merchandise[i].Price ()
<<endl;
cout<<"Wellcome to come again next time!";
_shopping.GetConsumer()[j].SetFlag(false);
}
}
}
return 0;
}

int main()
{

Shopping myClient;
Process process (myClient);
process.Run ();//error

return 0;
}
o1n 2004-10-17
  • 打赏
  • 举报
回复
//你原来那个根本不能通过编译的。你用的什么编译器,可以通过?????
#include <iostream>

#include <string>

using namespace std;
#include <stdlib.h>
#include <conio.h>

class Consumer
{
public:
Consumer ()
:_flag (true)
{
cout<<"consumer"<<endl;
}
void SetFlag (bool flag) { _flag = flag;}
bool GetFlag () {return _flag;}
private:
bool _flag;
};

class Goods
{
public:
Goods (string name, double price):_name (name),_price (price) {}//error
string Name () {return _name;}
double Price () {return _price;}
private:
string _name;
double _price;
};
Goods merchandise [5] = {Goods("Apple", 2.0),Goods("orange",1.8),Goods("pig",16.2),
Goods("Cake",6.4),Goods("pear",1.4)};//error


class Shopping
{
public:
Shopping ()
{}
Consumer * GetConsumer () {return _consumer;}
private:
Consumer _consumer [5];

};

class Process
{
public:
Process (Shopping shopping)
: _shopping (shopping)
{}
~Process ();
int Run ( );
private:
Shopping _shopping;

};//error

int Process::Run ()
{
while (getch() != 'Q')
{
cout<<"Wellcome our client!"<<endl;
for (int i = 0; i<5; ++i)
{
int j = rand ()*5;
if (_shopping.GetConsumer()[j].GetFlag() != false)
{
cout<<"You are our "<< i <<" client"
<<"Please come in"<<endl;
cout<<"OK,take your time."<<endl;
int kig = rand()*5;
int num = rand()*5;
cout<<"the "<<j<<" client"
<<"have bought" << kig
<<"Kilogram"<<merchandise[num].Name()
<<endl;
cout<<"OK,lets go to the counts!";
cout<<"Your should pay for "
<<kig * merchandise[i].Price ()
<<endl;
cout<<"Wellcome to come again next time!";
_shopping.GetConsumer()[j].SetFlag(false);
}
}
}
return 0;
}

int main()
{

Shopping myClient;
Process process (myClient);
process.Run ();//error

return 0;
}
cainiao0823 2004-10-17
  • 打赏
  • 举报
回复
我是一个刚开始学习c++的菜鸟,编了一个小程序
调试通过了,但运行的时候却崩溃了
那位大哥帮我看一下
谢谢!

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧