//class.cpp
#include
#include
#include
using std::string;
template
class Pair
{
private:
T1 a;
T2 b;
public:
Pair(const T1&aval,const T2&bval):a(aval),b(bval){}
Pair(){}
T1& first()const{return a;}
T2& second()const{return b;}
void set(const T1&u,const T2&i){a=u;b=i;}
void show(int y)
{for(int i=0;i<y;i++)
std::cout<<"\t\t"<<a[i]<<"\t"<<b[i]<<std::endl;
}
int sum(int y)
{int sum;
for(int i=0;i<y;i++)
sum+=b[i];
return sum;
}
};
typedef std::valarray ArrayInt;
typedef Pair PairArray;
class Wine
{
private:
string name;
PairArray array;
int year;
public:
Wine(const char*l,int y,const int yr[],const int bot[]);
Wine(const char*l,int y);
void GetBottles();
void Show();
string &Label();
int sum();
};
//函数定义
#include
#include"class.cpp"
using std::cout;
using std::cin;
using std::endl;
Wine::Wine(const char*l,int y,const int yr[],const int bot[])
{
name=l;
year=y;
ArrayInt m(y),n(y);
m=yr[y];
n=bot[y];
array.set(m,n);
}
Wine::Wine(const char*l,int y)
{
name=l;
year=y;
ArrayInt m(y),n(y);
array.set(m,n);
cout<<"Enter"<<name<<"data for"<<y<<" years:"<<endl;
}
void Wine::GetBottles()
{
ArrayInt a,b;
for(int i=0;i<year;i++)
{
cout<<"Enter years:";
cin>>a[i];
cout<<"Enter bottles for that year:";
cin>>b[i];
}
array.set(a,b);
}
void Wine::Show()
{
cout<<"Wine:"<<name<<endl;
cout<<"Year Bottles"<<endl;
array.show(year);
}
string& Wine::Label()
{
return name;
}
int Wine::sum()
{
return array.sum(year);
}
//main.cpp
#include
#include"class.cpp"
int main()
{
using std::cin;
using std::cout;
using std::endl;
cout<<"Enter name of wine:";
char lab[50];
cin.getline(lab,50);
cout<<"Enter number of year:";
int yrs;
cin>>yrs;
Wine holding(lab,yrs);
holding.GetBottles();
holding.Show();
const int YRS=3;
int y[YRS]={1993,1995,1998};
int b[YRS]={48,60,72};
Wine more("Gushing Grape Red",YRS,y,b);
more.Show();
cout<<"Total bottles for"<<more.Label()<<":"<<more.sum()<<endl;
cout<<"Bye\n";
return 0;
}
运行到第一个循环就失败了
不知道你这个问题是否已经解决, 如果还没有解决的话: