runtime stack overflow

hello,

How are you doing? I need a little help.

example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
template<class mynt>
class reverseiterator : public _Revranit<mynt, iterator<
	typename iterator_traits<mynt>::iterator_category,
	typename iterator_traits<mynt>::value_type,
	typename iterator_traits<mynt>::difference_type,
	typename iterator_traits<mynt>::pointer,
	typename iterator_traits<mynt>::reference>>
{

	typedef reverseiterator<mynt>  myt;
	typedef _Revranit<mynt, iterator<
	typename iterator_traits<mynt>::iterator_category,
	typename iterator_traits<mynt>::value_type,
	typename iterator_traits<mynt>::difference_type,
	typename iterator_traits<mynt>::pointer,
	typename iterator_traits<mynt>::reference>> mybase;
public:
	typedef typename iterator_traits<mynt>::difference_type difference;
	typedef typename iterator_traits<mynt>::pointer pointer;
	typedef typename iterator_traits<mynt>::reference reference;

	reverseiterator(){}

	reverseiterator(mynt right):mybase(right){}

	template<class other>
	reverseiterator(reverseiterator<other> &r):mybase(r.base()){}

	reverseiterator(mybase right):mybase(right){}


	myt& operator++()
	{
		++*this;          //ERROR
		return *this;
	}

	myt& operator++(int)
	{
		myt tmp = *this;
		++this;
		return tmp;
	}

	myt& operator--()
	{
		--*this;          //ERROR
		return *this;
	}

	myt& operator--(int)
	{
		myt tmp = *this;
		++*this;
		return tmp;
	}

	myt& operator+=(difference off)
	{
		*this += off;
		return *this;
	}

	myt operator +(difference off)const
	{
		myt tmp = *this;
		return tmp += off;
	}

	myt& operator-=(difference off)
	{
		*this -= off;
		return *this;
	}

	myt operator -(difference off)const
	{
		myt tmp = *this;
		return tmp -= off;
	};


};




'reverseiterator<std::_Vector_iterator<std::_Vector_val<int,std::allocator<int> > > >::operator++' : recursive on all control paths, function will cause runtime stack overflow
'reverseiterator<std::_Vector_iterator<std::_Vector_val<int,std::allocator<int> > > >::operator--' : recursive on all control paths, function will cause runtime stack overflow


What is wrong with this code?

thank in advance.
Last edited on
what are you trying to do?

Yes it is recursive and the operators += and -= are as well. You cannot overload an operator and then using that operator within that operator.
Your ++ and -- operators call themselves.

Doing this:

1
2
3
4
5
6
	myt& operator++()
	{
		++*this;          //ERROR
		return *this;
	}


is the same as doing this:

1
2
3
4
void func()
{
  func();
}


func will call func, which calls func again, which calls it again, and again, and again, etc. It'll never stop calling itself which creates an infinite loop and eats up all your stack space until the program dies.

Your += and -= operators have the same problem.

Instead of having the operator call itself, you are supposed to implement what you want that operator to do. What does it mean to do ++obj;? Write the code that answers that question for that operator. Then do the same for --, +=, and -=.
1
2
3
4
5
	myt& operator++()
	{
		++*((mybase *)this);
		return *this;
	}



I do not understand.

I write as above and the problem was solved
why ?
I write as above and the problem was solved
why ?
Because then you called the operator of the base class which is a different one

You can also write mybase::operator++()
I understand now.How can I fix the code ??
thank everyone you for your responses.
if those operators are already present in the base class and you don't want to add functionality. I'd say omit them?

But still I don't know what you're trying to do...

This is how you can actually write it
1
2
3
4
	myt& operator++()
	{
		return mybase::operator++();
	}
and the other operators accordingly
thanks coder777
Topic archived. No new replies allowed.