Strange class problem

Hello all

I have a head basher, since I think the solution is very easy and obvious but I simply can't find the error. Here is my code

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
class Point{
	int x, y, z;

	public:
		Point();
		Point(int , int , int );
		void SetArea(int, int, int);
		void ShowArea(void);
};

Point::Point(){
	cout << "this is a default counstructor" << endl;
}
Point::Point(int a = 1, int b = 1, int c = 1){
	x = a;
	y = b;
	z = c;

	cout << "this is our constructor, and the values are: " << endl;
	cout << "x = " << x << "\ny = " << y << "\nz = " << z << endl;

}

class CRectangle {
    int width, height;
  public:
    CRectangle ();
    CRectangle (int,int);
    int area (void) {return (width*height);}
};

CRectangle::CRectangle () {
  width = 5;
  height = 5;
}

CRectangle::CRectangle (int a, int b) {
  width = a;
  height = b;
}


int _tmain(int argc, _TCHAR* argv[]){
	funct();

        return 0;
}


void funct(void){

	CRectangle rect (3,4);
	CRectangle rectb;

	cout << "rect area: " << rect.area() << endl;
	cout << "rectb area: " << rectb.area() << endl;

	getchar();


	Point myPoint(3,4);
	Point hisPoint;

	cout << "function \"funct\" finished succesfully" << endl;
	getchar();
	exit(0);
}


This is the code, now onto the problem, as you can see, or as far as I can see, classes Point and CRectangle are very simmiliar as far as counstructors go, they both have 2 of them, so they're obviously overloaded, but what bogles my mind is the error I get when compiling:

"error C2668: 'Point::Point' : ambiguous call to overloaded function"
"could be 'Point::Point(int,int,int)"
"or 'Point::Point(void)"

the error is with this line Point hisPoint;
which to me seems not all that different from
CRectangle rectb;

I know what the error is sopouse to mean, basically that the compiler doesn't know which function to call, and that I need to specify it more clearly with the arguments,
But I don't see how the CRectangle works then, since I can not find any differences and, he works like a charm.

Hopefully I was at least somewhat clear as to my problem,

any help would be appreciated.

Thanks guys.

The line causing the problem is this one -> Point myPoint(3,4);

Neither of the Point constructors takes two ints as arguments:

1
2
Point();
Point(int , int , int );

Last edited on
But Point(int,int,int) has default 3rd parameter.

I think the problem is here
Point::Point()
and
Point::Point(int a = 1, int b = 1, int c = 1)
The compiler cannot decide which constructor should be called because they both can be called with the same syntax:
Point hisPoint;

One of the ways to solve this is to remove one default parameter:
Point::Point(int a , int b = 1, int c = 1);

See http://msdn.microsoft.com/en-us/library/da60x087(VS.80).aspx
I don't think so, I commented it out, and still the same problem persists.
Why I think this like is not a problem is because I have this decleration
Point::Point(int a = 1, int b = 1, int c = 1)
and he simply puts the default values in, if there are less then 3 arguments....

Hehe, while I was writing this, I figured it out, I don't quite understand it, but since I've given this constructor Point::Point(int a = 1, int b = 1, int c = 1) it's default values in case of no argument input, he simply wants to call this constructor no matter what I do, but since I have a deafult constructor which takes no values, he then does not know which one to use, what I did now is simply removed the default argument values from this Point::Point(int a = 1, int b = 1, int c = 1) to this Point::Point(int a , int b , int c ) and it seems to work now.

Thanks for your help :D

cheers mate.
thanks null, yeah I got it, I was just replying to m4ster r0shi and I didn't see your comment.

Thanks again

nice one :)
Null wrote:
But Point(int,int,int) has default 3rd parameter.

Oops... I didn't notice that...

Null's explanation +1
Topic archived. No new replies allowed.