关于静态数据成员问题?求解答!

C++语言 码拜 9年前 (2015-05-11) 959次浏览 0个评论

#include <iostream> 
#include<iomanip>
using namespace std;
const  int MAX=100; 
const  int N=8;

class Order
{
private:
int num; //订单编号 
string name; //订单名称 
float price; //订单价格 
int quantity; //订单数量 
static int cout;
static double total;
public:
void Input();
void Output();
template<class T>
  T judge(T a)
{

while(a<0)
{
cout<<“输入不合法,重新输入:”;
cin>>a;
}
return a;
}
};
//int Order::cout=0;
//double Order::total=0;
void Order::Input()
{
cout<<“请输入订单编号:”;
cin>>num;
cout<<“请输入订单名称:”;
cin>>name;
cout<<“请输入订单价格:”;
cin>>price;
price=judge(price); 
 cout<<“请输入订单数量:”;
 cin>>quantity;
 quantity=judge(quantity);
}
void Order::Output()
{
cout<<setw(N)<<“编号”<<setw(N)<<“名称”<<setw(N)<<“价格”<<setw(N)<<“数量”<<endl;
cout<<setw(N)<<num<<setw(N)<<name<<setw(N)<<price<<setw(N)<<quantity<<endl; 
}

int main()
{ Order c;
c.Input();
c.Output();
return 0;
}为什么加上静态数据成员以后就不能运行了?关于静态数据成员问题?求解答!

5分
1、代码请格式化后,用”

 

“框起来
2、提问有艺术。现在完全不知道想问什么

能解释一下吗,或者举一个例子,初学者不懂
修改如下:

#include <iostream> 
#include <iomanip>
#include <string>
using namespace std;

template<class T>
T judge(T a)
{
	while (a<0)
	{
		cout << "输入不合法,重新输入:";
		cin >> a;
	}
	return a;
}
const int MAX = 100;
const int N = 8;

class Order
{
private:
	int num;	     //订单编号 
	string name;	 //订单名称 
	float price;	 //订单价格 
	int quantity;	 //订单数量 
	static int cout;
	static double total;
public:
	void Input();
	void Output();
};

int Order::cout=0;
double Order::total=0;

void Order::Input()
{
	std::cout << "请输入订单编号:";
	cin >> num;
	std::cout << "请输入订单名称:";
	cin >> name;
	std::cout << "请输入订单价格:";
	cin >> price;
	price = judge(price);
	std::cout << "请输入订单数量:";
	cin >> quantity;
	quantity = judge(quantity);
}

void Order::Output()
{
	std::cout << setw(N) << "编号" << setw(N) << "名称" << setw(N) << "价格" << setw(N) << "数量" << endl;
	std::cout << setw(N) << num << setw(N) << name << setw(N) << price << setw(N) << quantity << endl;
}


int main()//21
{
	Order c;
	c.Input();
	c.Output();

	return 0;
}
25分
你自定义了一个cout,所以会有std::cout的代码出现。
其实你应该把

static int cout;

改为

static int count;
cout 是关键字

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于静态数据成员问题?求解答!
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!