getline: unresolved overloaded function type

I am using code from The c++ standard library from scratch, I wanted to use getline instead of just typing it directly into the testfile..however I run into

unresolved overloaded function.

I appreciate the help. I included the header and implementation file along with the testfile.

Thanks

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
 


 
#include <string>


class Address
{
public:

std::string lastname() const{return lastname_;}
void lastname(const std::string&);

std::string firstname() const{return firstname_;}
void firstname(const std::string&);

std::string phone() const{return phone_;}
void phone(const std::string&);

std::string address() const{return address_;}
void address(const std::string&);

private:

std::string lastname_;
std::string firstname_;
std::string phone_;
std::string address_;

};





/*

header
*/

#include "Address.h"

void Address::lastname(const std::string& s)
{
lastname_=s;
}

void Address::firstname(const std::string& s)
{
firstname_=s;
}

void Address::phone(const std::string& s)
{
phone_=s;
}

void Address::address(const std::string& s)
{
address_=s;
}



/*
test file
*/




#include <iostream>

#include "Address.h"
using namespace std;



void dump(const Address& a)
{
std::cout<<a.firstname() <<' '<<a.lastname() << '\n'<<a.address() << '\n' <<a.phone()<< '\n'
<<std::endl;
}

int main()
{
Address a;
std::getline (std::cin,a.lastname);
std::getline (std::cin,a.firstname);
std::getline (std::cin,a.phone);
std::getline (std::cin,a.address);

dump(a);

return 0;

}


std::getline (std::cin,a.lastname);
My first bet is that you used a function without its brackets.

lastname is a function, so it should be called like so:
std::getline (std::cin,a.lastname());
Last edited on
std::getline expects a std::string as argument.

1
2
3
std::string lastname;
std::getline (std::cin, lastname);
a.lastname(lastname);
closed account (E0p9LyTq)
std::getline() requires an input stream and a std::string. You supplied std::cin (correct) and class functions (incorrect). Class functions that require a string as input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
   Address a;
   std::string temp;

   std::getline(std::cin, temp);
   a.lastname(temp);

   std::getline(std::cin, temp);
   a.firstname(temp);

   std::getline(std::cin, temp);
   a.phone(temp);

   std::getline(std::cin, temp);
   a.address(temp);

   std::cout << '\n';

   dump(a);
}

Schmoe
Joe
111-222-3333
123 Nowhere Road

Joe Schmoe
123 Nowhere Road
111-222-3333
Last edited on
Thank you so much! Your explanations were very thoughtful and I am able to get more out of my book.

Thanks again, these small victories keep my anxiety at bay!

Topic archived. No new replies allowed.