Runtime Stack Overflow

Here is my error message:
Warning 1 warning C4717: 'getx' : recursive on all control paths, function will cause runtime stack overflow

and here is my code I will Bold the part that is giving me problems!

Can anyone help me please?!?!?!?!

class point
{
public:
point();
point(int xx, int yy);
friend double distance(point p1, point p2);
int getx();
int gety();
double dist;
double xnum, ynum, total;


private:
int x, y;

};

point::point()
{

x = 1;
y = 1;

}

point::point(int xx, int yy)
{
x = xx;
y = yy;
}

double distance(point p1, point p2)
{
double dist;
double xnum, ynum, total;


xnum = (p2.getx() - p1.getx());
xnum = pow (xnum, 2.0);

ynum = (p2.gety() - p1.gety());
ynum = pow (ynum, 2.0);

total = xnum + ynum;

dist = sqrt (total);

return dist;
}

int getx()
{
return getx();
}

int gety()
{
return gety();
}

1
2
3
4
int getx()
{
    return getx();
}

calls itself infinitely.

When your code calls getx(), it jumps to the getx() function and executes whatever's in the function. When it gets to a return statement, it jumps back to whatever comes straight after the function call. But, your getx() and gety() functions call themselves infinitely. Because every time you call a function it uses a little bit of memory, this function will eventually want more memory than the operating system will give it, resulting in stack overflow.
Thanks for the reply,

so how would I go about fixing that? Can I add a break after the return statement, and if I do add a break will it still allow me to get two values?
Or do I need to use a while statement, so it will only take values that the user input?
I think you're trying to do something like this:
1
2
3
4
5
6
7
8
9
int point::getx ()
{
    return x;
}

int point::gety ()
{
    return y;
}
.

Is that right?
Yes I tried it and if fix the error, but I'm getting back the wrong results!

Thanks a lot!

If you see anything else that would cause me to get the wrong output, could you let me know?

Here is my .cpp file:

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

#include "quiz2.h"

using namespace std;

int main()
{
	point c;

	int x , xx;
	int y, yy;

	cout <<"Enter two points: ";
	cin >> x >> xx;

	c.getx();

	cout <<"Enter two points: ";
	cin >> y >> yy;

	c.gety();

	cout <<"***** The distance between (x1, y1) and (x2, y2) is: " <<c.dist <<"*****" <<endl;

	system("pause");

	return 0;
}
basically you're not setting the data stored in 'point c' to anything. In class point you need to define another 2 functions, void setx (int) and void sety (int) so that you can actually store data in the class.
Topic archived. No new replies allowed.