首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

类模板中的一个小疑点 找不到原因?

2012-03-18 
类模板中的一个小问题 找不到原因??应该把Negativeindex输出,可是为什么输出的是Indextoolarge??????#ifnd

类模板中的一个小问题 找不到原因??
应该把Negative   index输出,可是为什么输出的是Index   too   large??????
#ifndef   Array_H
#define   Array_H
#include <stdexcept>
template   <typename   T>   class   Array{
private:
T*   elements;
size_t   size;
public:
    explicit     Array <T>   (size_t   arraySize);
                      Array <T>   (const   Array <T> &   theArray);
    ~Array <T> ();
    T&   operator[](size_t   index);
    const   T&   operator[](size_t   index)   const;
    Array <T> &   operator=(const   Array <T> &   rhs);
};//类模板
template   <typename   T>   Array <T> ::Array(size_t   arraySize):size(arraySize){
elements=new   T[size];
}//构造函数
template   <typename   T>     Array <T> ::Array(const   Array   &theArray){
size=theArray.size;
elements=new   T[size];
for(int   i=0;i <size;i++)
elements[i]=theArray.elements[i];
}//副本构造函数
template   <typename   T>   Array <T> ::~Array(){
delete[]   elements;
}//析构函数
template   <typename   T>   T&   Array <T> ::operator   [](size_t   index){
if(index <0||index> =size)
throw   std::out_of_range(index <0? "Negative   index ": "Index   too   large ");
return   elements[index];
}//重载下标运算符非const   类型
template   <typename   T> const   T&   Array <T> ::operator   [](size_t   index)   const{
if(index <0||index> =size)
throw   std::out_of_range(index <0? "Negative   index ": "Index   too   large ");
return   elements[index];
}//重载下标运算符   const   类型
template   <typename   T>   Array <T> &   Array <T> ::operator   =(const   Array&   rhs){
if(this=&rhs)
return   *this;
if(elements)
delete[]   elements;
size=rhs.size;
elements=new   T[size];
for(i=0;i <size;i++)
elements[i]=rhs.elements[i];
}//重载赋值运算符
#endif
_______________________________________Array.h___________________________
#ifndef   Box_H
#define   Box_H
class   Box{
public:
double   volume(double   a=1,double   b=1,double   c=1);
private:
double   a;
double   b;
double   c;
};
double   Box::volume(double   a,double   b,double   c)
{
return   a*b*c;
}
#endif
_________________________________Box.h__________________________________
#include   "Box.h "
#include   "Array.h "
#include   <iostream>
#include   <iomanip>
using   std::cout;
using   std::endl;
int   main()
{
const   int   doubleCount=50;
Array <double>   values(doubleCount);
try{
for   (int   i=0;i <doubleCount;i++)
values[i]=i+1;
cout < <endl < < "Sums   of   pairs   of   elements; ";
int   lines=0;
for(i=doubleCount-1;i> =0;i--)
cout < <(lines++   %   5==0? "\n ": " ") < <std::setw(5)


< <values[i]+values[i-1];
/*问题是当i为-1时调用   Arrary.h中template   <typename   T>   T&   Array <T> ::operator   [](size_t   index)重载[]函数   因为i=-1时下标越界要抛异常按throw   std::out_of_range(index <0? "Negative   index ": "Index   too   large ");这条语句应该把Negative   index输出,可是为什么输出的是Index   too   large??????*/
}
catch(const   std::out_of_range&   ex){
cout < <endl < < "out_of_range   exception   object   caught! " < <ex.what();
}
try{
const   int   boxCount=10;
Array <Box> boxes(boxCount);
for(int   i=0;i <=boxCount;i++)
cout < <endl < < "Box   volume   is " < <boxes[i].volume();
}
catch(const   std::out_of_range&   ex){
cout < <endl < < "out_of_range   exception   object   caught   ! " < <ex.what();
}
cout < <endl;
return   0;
}

[解决办法]
size_t是无符号数!

热点排行