phone number class with demo

I have created a class to hold phone numbers and everything works except my constructor that takes parameters. In my main when I try to call the constructor with a missing fourth parameter, the string variable, (there is a default class that should be giving it the default value) it says there is no matching function for it. It should call the constructor that takes 4 parameters but it is not. The issue occurs at line 17 of main. Any help would be appreciated.

main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>

#include "phone_number.h"

using namespace std;

int main() {
	PhoneNumber phone_number(123, 456, 7890, "Home");
	cout<< phone_number << endl;
	cout << phone_number.get_area_code() << endl;
	cout << phone_number.get_exchange() << endl;
	cout << phone_number.get_subscriber() << endl;

	PhoneNumber phone_number2;
	cout << phone_number2 << endl;

	cout << PhoneNumber(234, 567, 8901) << endl;

	return 0;
}


.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef PHONE_NUMBER_H
#define PHONE_NUMBER_H

#include <iostream>
#include <string>

using namespace std;

class PhoneNumber{
    friend std::ostream &operator <<(std::ostream &os, const PhoneNumber &phone);
public:
    PhoneNumber();
    PhoneNumber(int, int, int, string);
    int get_area_code();
    int get_exchange();
    int get_subscriber();
    string get_category();
private:
    int area, exchange, subscriber;
    string category;
};
#endif


.cpp
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
#include "phone_number.h"
#include <iostream>
#include <string>

using namespace std;

ostream &operator <<(std::ostream &os, const PhoneNumber &phone)
{
    os << "(" << phone.area << ")" << phone.exchange << "-" << phone.subscriber << " (" << phone.category << ")";
    return os;
}

PhoneNumber::PhoneNumber()
{
    area=0;
    exchange=0;
    subscriber=0;
    category="";
}

PhoneNumber::PhoneNumber(int x, int y, int z, string a)
{
    area=x;
    exchange=y;
    subscriber=z;
    category=a;
}
a
int PhoneNumber::get_area_code()
{
    return area;
}

int PhoneNumber::get_exchange()
{
    return exchange;
}

int PhoneNumber::get_subscriber()
{
    return subscriber;
}

string PhoneNumber::get_category()
{
    return category;
}
Last edited on
I don't see any default parameter values anywhere.

try this at line 13 in the .h file
PhoneNumber(int, int, int, string a = "defaultValue");
thanks, that worked
Topic archived. No new replies allowed.