Help with default constructors

This is what I have so far:

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>

using namespace std;

class Pairs
{
public:
	Pairs(short first = 0, short second = 0);

	friend ostream& operator <<(ostream& outs, const Pairs pair);

private:
	short f;
	short s;
};

int main()
{
	char loop = 'y';

	do
	{
		Pairs xy();
		cout << xy << endl;

		cout << "Would you like to run the program again? (y/n)? ";
		cin >> loop;
		loop = tolower(loop);
		while(loop != 'y' && loop != 'n')
		{
			cout << "Incorrect output. Would you like to run the program again? ";
			cin >> loop;
		}
	}while(loop == 'y');

	cout << "Press a key to exit: ";
	char exit;
	cin >> exit;

	return 0;
}

Pairs::Pairs(short first, short second): f(first), s(second)
{

}

ostream& operator <<(ostream& outs, const Pairs pair)
{
	outs << '(' << pair.f << ',' << pair.s << ')';
	return outs;
}


When I compile the code as is it gives me an error:

1> LINK : C:\Users\Paul\Documents\Visual Studio 2010\Projects\test4\Debug\test4.exe not found or not built by the last incremental link; performing full link
1>test4.obj : error LNK2019: unresolved external symbol "class Pairs __cdecl xy(void)" (?xy@@YA?AVPairs@@XZ) referenced in function _main
1>C:\Users\*****\Documents\Visual Studio 2010\Projects\test4\Debug\test4.exe : fatal error LNK1120: 1 unresolved externals

However it goes away when I change line 26 so that it has one or two arguments, such as, for e.g.: Pairs xy(1) or Pairs xy(1,1). I guess this has something to do with the fact that I don't have a default constructor but how come line 11 doesn't take care of that for me? I have two default arguments -- Do I still need to make a default constructor despite this? I thought the two default arguments in line 11 could make the function behave as a default constructor.
Hello, bool maybe.

If the default constructor is overloaded (not the copy constructor), the default constructor is replaced with the one (s) that you defined. So when an instantiation of Pairs is made, you must use the constructor you defined. As a result, your instantiation declaration should look like this:

 
Pairs XY( 0, 10 );

Last edited on
No, just remove the () if you want to call the default constructor. Pairs xy;
Mathhead, Thanks, that did it. I think you helped me yesterday too so thanks again.
Topic archived. No new replies allowed.