C++程序错误,内容涉及类模板和类包含,多文件

woomevan 2012-07-20 04:29:53

//pair.h
#ifndef PAIR_H_
#define PAIR_H_
template<class T1,class T2>
class Pair
{
private:
T1 a;
T2 b;
public:
T1 &first();
T2 &second();
T1 first()const
{
return a;
}
T2 second()const
{
return b;
}
Pair(const T1 &aval,const T2 &bval):a(aval),b(bval){}
Pair(){}
};
#endif

//pair.cpp
#include"pair.h"
template<class T1,class T2>
T1 &Pair<T1,T2>::first()
{
return a;
}
template<class T1,class T2>
T2 &Pair<T1,T2>::second()
{
return b;
}

//winec.h
#ifndef WINEC_H_
#define WINEC_H_
#include<string>
#include<valarray>
#include"pair.h"
class Wine
{
protected:
typedef std::valarray<int> arrayint;
typedef Pair<arrayint,arrayint> pairarray;
private:
std::string s;
pairarray p;
int year;
public:
Wine(const char *l,int y,const int yr[],const int bot[]);
Wine(const char *l,int y);
void getbottles();
const std::string &label()const;
int sum()const;
void show()const;
};
#endif

//winec.cpp
#include<string>
#include<iostream>
#include<cstdlib>
#include<valarray>
#include"winec.h"
using std::cout;
using std::endl;
using std::cin;
Wine::Wine(const char *l,int y,const int yr[],const int bot[])
:s(l),year(y),p(arrayint(yr,y),arrayint(bot,y))
{}
Wine::Wine(const char *l,int y):s(l),year(y),p(arrayint(y),arrayint(y))
{}
void Wine::getbottles()
{
cout<<"enter gully wash data fore 4 years(s):"<<endl;
int i=0;
do
{
cout<<"enter year: ";
if(!cin>>p.first()[i])
exit(EXIT_FAILURE);
while(cin.get()!='\n');
cout<<"enter bottles for that year: ";
if(!cin>>p.second()[i++])
exit(EXIT_FAILURE);
while(cin.get()!='\n');
}
while(i<4);
}
const std::string &Wine::label()const
{
return s;
}
int Wine::sum()const
{
return p.second().sum();//我的编译器这里报错
}
void Wine::show()const
{
cout<<"wine: "<<s<<endl
<<"year\tbottle\n";
for(int i=0;i<year;++i)
cout<<p.first()[i]<<'\t'<<p.second()[i]<<endl;
}

//temp.cpp
#include<iostream>
#include"winec.h"
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 name of years: ";
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;
return 0;
}
...全文
46 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

64,650

社区成员

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

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