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

小弟我自己定义的一个类,测试输出出错,求教大侠

2013-01-07 
我自己定义的一个类,测试输出出错,求教大侠输入10个数字后,控制台显示“你的CheckPtr对象是:”,后来就出不来

我自己定义的一个类,测试输出出错,求教大侠
输入10个数字后,控制台显示“你的CheckPtr对象是:”,后来就出不来了,不知道问题出在哪。。。。。


#include <iostream>

#ifndef CHECKPTR_H
#define CHECKPTR_H
class CheckPtr
{
friend std::ostream& operator<<(std::ostream&, const CheckPtr&);
public:
CheckPtr& operator++();
const CheckPtr& operator++(int);
CheckPtr& operator--();
const CheckPtr& operator--(int);
bool operator==(const CheckPtr&);
bool operator!=(const CheckPtr&);
bool operator<(const CheckPtr&);
bool operator>(const CheckPtr&);
bool operator<=(const CheckPtr&);
bool operator>=(const CheckPtr&);
const CheckPtr& operator+(const unsigned&);
const CheckPtr& operator-(const unsigned&);
CheckPtr& operator=(const CheckPtr&);
CheckPtr& operator+=(const unsigned&);
CheckPtr& operator-=(const unsigned&);
int& operator*(CheckPtr&);
const int& operator*(const CheckPtr&);
CheckPtr(int *pb, int *pe): beg(pb), end(pe), cur(pb) {}
CheckPtr(const CheckPtr &ptr): beg(ptr.beg), end(ptr.end), cur(ptr.cur) {}
~CheckPtr() { delete beg; delete end; delete cur; }
private:
int *beg;
int *end;
int *cur;
};

#endif



#include "CheckPtr.h"
#include <iostream>

CheckPtr& CheckPtr::operator++()
{
if (cur == end){
std::cout << "超出范围!" << std::endl;
return *this;
}
++cur;
return *this;
}

const CheckPtr& CheckPtr::operator++(int)
{
CheckPtr ret(*this);
++*this;
return ret;
}

CheckPtr& CheckPtr::operator--()
{
if (cur == beg){
std::cout << "超出范围!" << std::endl;
return *this;
}
--cur;
return *this;
}

const CheckPtr& CheckPtr::operator--(int)
{
CheckPtr ret(*this);
--*this;
return ret;
}

bool CheckPtr::operator==(const CheckPtr &ptr)
{
if (beg == ptr.beg && end == ptr.end && cur == ptr.cur)
return true;
else
return false;
}

bool CheckPtr::operator!=(const CheckPtr &ptr)
{
if (beg != ptr.beg || end != ptr.end || cur != ptr.cur)
return true;
else
return false;
}

//对于不指向同一数组的,进行小于关系比较,始终返回假。
//其他关系比较也相同
bool CheckPtr::operator<(const CheckPtr &ptr)
{
if (beg == ptr.beg && end == ptr.end)
{
if (cur < ptr.cur)
return true;
else
return false;
}
else
return false;
}

bool CheckPtr::operator>(const CheckPtr &ptr)
{
if (beg == ptr.beg && end == ptr.end)
{
if (cur > ptr.cur)
return true;
else
return false;
}
else
return false;
}

bool CheckPtr::operator<=(const CheckPtr &ptr)


{
if (beg == ptr.beg && end == ptr.end)
{
if (cur <= ptr.cur)
return true;
else
return false;
}
else
return false;
}

bool CheckPtr::operator>=(const CheckPtr &ptr)
{
if (beg == ptr.beg && end == ptr.end)
{
if (cur >= ptr.cur)
return true;
else
return false;
}
else
return false;
}

const CheckPtr& CheckPtr::operator+(const unsigned &ix)
{
if (cur + ix < end)
cur += ix;
return *this;
}

const CheckPtr& CheckPtr::operator-(const unsigned &ix)
{
if (cur - ix >= beg)
cur -= ix;
return *this;
}

CheckPtr& CheckPtr::operator=(const CheckPtr &ptr)
{
beg = ptr.beg;
end = ptr.end;
cur = ptr.cur;
return *this;
}

CheckPtr& CheckPtr::operator+=(const unsigned &ix)
{
if (cur + ix < end)
cur += ix;
return *this;
}

CheckPtr& CheckPtr::operator-=(const unsigned &ix)
{
if (cur - ix >= beg)
cur -= ix;
return *this;
}

const int& CheckPtr::operator*(const CheckPtr &ptr)
{
return *ptr.cur;
}

int& CheckPtr::operator*(CheckPtr &ptr)
{
return *(ptr.cur);
}

std::ostream& operator<<(std::ostream &os, const CheckPtr &ptr)
{
os << *ptr.cur;
return os;
}




#include "CheckPtr.h"
#include <iostream>
using namespace std;

int main()
{
cout << "请输入10个整数:" << endl;
int arr[10];
for (unsigned i = 0; i != 10; ++i)
cin >> arr[i];
CheckPtr arr_ptr(arr, arr + 9);
cout << "你的CheckPtr对象的数字是:" << endl;
for (unsigned num = 0; num != 9; ++num)
cout << arr_ptr++ << "\t";
cout << endl;
return 0;
}

[解决办法]
好像没什么问题,正常都打印出来了。
1       2       3       4       5       6       7       8       9

热点排行