c++stl基础及应用中的第2章的代码,无法实现,似乎是有一点问题,请指教
#include<iostream>#include<string>usingnamespacestd;classCintArray{inta[10];public:Ci...
#include <iostream>#include <string>using namespace std;class CintArray{ int a[10];public : CintArray() { for(int i=0;i<10;i++) a[i] = i+1; } int Getsum(int times) { int sum = 0; for(int i=0;i<10;i++) sum+=a[i]; return sum*times; }};class Cfloatarray{ float f[10];public: Cfloatarray() { for(int i=1;i<=10;i++) { f[i-1] = 1.0f/i; } } float Getsum(float times) { float sum = 0.0; for(int i=0;i<10;i++) { sum +=f[i]; } return sum*times; }};template<class T>class NumTraits{
};template<>class NumTraits<CintArray>{
public:typedef int resulttype;typedef int inputpara;};template<>class NumTraits<Cfloatarray>{ //friend class Capply;public: typedef float resulttype; typedef float inputpara;};template<class T>class Capply{public: NumTraits<T>::resulttype Getsum(T &obj,NumTraits<T>::inputpara in) { return obj.Getsum(in); }};int main(){ CintArray intary; Cfloatarray fltary; Capply<CintArray> c1; Capply<Cfloatarray> c2; cout<<"c1"<<c1.Getsum(intary,3)<<endl; cout<<"c2"<<c2.Getsum(fltary,3.2f)<<endl; return 0;}错误提示: 展开
};template<>class NumTraits<CintArray>{
public:typedef int resulttype;typedef int inputpara;};template<>class NumTraits<Cfloatarray>{ //friend class Capply;public: typedef float resulttype; typedef float inputpara;};template<class T>class Capply{public: NumTraits<T>::resulttype Getsum(T &obj,NumTraits<T>::inputpara in) { return obj.Getsum(in); }};int main(){ CintArray intary; Cfloatarray fltary; Capply<CintArray> c1; Capply<Cfloatarray> c2; cout<<"c1"<<c1.Getsum(intary,3)<<endl; cout<<"c2"<<c2.Getsum(fltary,3.2f)<<endl; return 0;}错误提示: 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询