Need help with class constructors.

I can't figure out how to define a Customer constructor that takes two strings. I need this constructor to get this class to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  class Customer {
	const std::string custName, custAddress;
public:
	void custInfo(std::ostream &) const
	{
		std::cout << "Name" << custName << "\n"
			"Address" << custAddress << "\n" << std::endl;
	}
	void custWelcome(std::ostream &) const
	{
		std::cout << "\nHello " << custName << "\nAddress: " << custAddress << std::endl;
	}
	std::string custValue() const;
	Customer(const std::string &custName, const std::string &custAddress);
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Customer {
	const std::string custName, custAddress;
public:
	void custInfo(std::ostream &) const
	{
		std::cout << "Name" << custName << "\n"
			"Address" << custAddress << "\n" << std::endl;
	}
	void custWelcome(std::ostream &) const
	{
		std::cout << "\nHello " << custName << "\nAddress: " << custAddress << std::endl;
	}
	std::string custValue() const;
	Customer(const std::string &custName, const std::string &custAddress)
        {
              // your code here
         }
};
I can't figure out how to define a Customer constructor that takes two strings.

You already have. It's at lines 14 - 17.

Can you be more specific about what problem you are having?

EDIT: I suspect what you're trying to do is initialise the object's data members. I recommend looking at:

http://www.cplusplus.com/doc/tutorial/classes/

and in particular the section on Member initialization in constructors.
Last edited on
Okay I put that there but what would fill in where
//your code here
goes I am confused by this?
I don't know. What code do you want to put there? What is it that you want your constructor to do?
Well the class is used for the customer and called to display the name and address so. My program should ask you to enter your name and address then should say :
Hello Mikey
Address: Mikey's address

So I am confused what should be used here.
You are passing two strings to the constructor. Clearly, you want one of those strings to be used for the name, and one for the address. The class has two string variables inside it, clearly meant to be used for the name and address.

Here is how we set one string to have the same value as another value;

someString = otherString;

I'll do one for you:


1
2
3
4
5
	Customer(const std::string &inputCustName, const std::string &inputCustAddress)
        {
              custName = inputCustName;
              // Can you work out the other one?
         }

So like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Customer {
	const std::string custName, custAddress;
public:
	void custInfo(std::ostream &) const
	{
		std::cout << "Name" << custName << "\n"
			"Address" << custAddress << "\n" << std::endl;
	}
	void custWelcome(std::ostream &) const
	{
		std::cout << "\nHello " << custName << "\nAddress: " << custAddress << std::endl;
	}
	std::string custValue() const
	{
			return custName;
	}

	Customer(const std::string &inputCustName, const std::string &inputCustAddress)
	{
		custName = inputCustName;
		custAddress = inputCustAddress;
	}
};


Problem is lines 26-27 say no operator "=" matches these operands.

What is the issue?
I did the "==" no error but when I build it says operator has no effect did you mean "=" but when I do "=" no operator matches. So what am I doing wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Customer {
	const std::string custName, custAddress;
public:
	void custInfo(std::ostream &) const
	{
		std::cout << "Name" << custName << "\n"
			"Address" << custAddress << "\n" << std::endl;
	}
	void custWelcome(std::ostream &) const
	{
		std::cout << "\nHello " << custName << "\nAddress: " << custAddress << std::endl;
	}
	std::string custValue() const
	{
			return custName;
	}

	Customer(const std::string &inputCustName, const std::string &inputCustAddress)
	{
		custName == inputCustName;
		custAddress == inputCustAddress;
	}
};
Last edited on
So what am I doing wrong?

Fundamentally, trying to program by guessing at what to type and hoping that when the compiler stops complaining, it will do what you want. That's a really bad way to program.

 
const std::string custName, custAddress;

You've made these const. Which means you can't change them. Which means you can't assign them a value inside the constructor. You could only assign them a value using an "initialiser list".

I recommend you just make them non-const. Initialiser lists might be a bit tricky for you at the moment.
Initialiser lists might be a bit tricky for you at the moment.

I both agree and disagree.

Why didn't you show him the proper initialization syntax in the first place?
It may be "challenging", but it is the Right Thing to do.

1
2
3
4
Customer(const std::string &inputCustName, const std::string &inputCustAddress)
: custName( inputCustName ), custAddress( inputCustAddress )
{
}


Whatever one does in the body of a constructor is not initialization. Initialization happens before the body.


PS. What is the difference between = and ==?
Why didn't you show him the proper initialization syntax in the first place?


It is a constant discussion I have inside my own head. There's a trade-off to make between what the person (typically) wants (i.e. a fast answer that just works) and a more expansive answer that will be harder to understand and possibly confusing, but is a better answer from a technical C++ perspective.

Answer too simply and I'm not helping them become a better programmer, answer too far in the other direction and they won't understand or won't care and will just give up on it. The "best" answer changes depending on a number of different circumstances and I'm sure that I get it wrong about 50% of the time.
Why didn't you show him the proper initialization syntax in the first place?

In my response to the OP yesterday, I pointed them to the specific part of the tutorial that discusses initialiser lists, with examples of the syntax. The OP responded:

josephhoffman28 wrote:
Okay I put that there

Returning to the thread today, I'm a bit surprised to see that they didn't, in fact, put an initialiser list there, so I'm kinda wandering what it was they actually meant...
Last edited on
Okay,

@josephhoffman28:
Here is an another text that focuses on initialization: http://www.informit.com/articles/article.aspx?p=1852519

Please tell, if it seems utterly confusing.
I believe I figured out the code I read that as well:

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit

class Customer {
	const std::string custName, custAddress;
public:
	void custInfo(std::ostream &) const
	{
		std::cout << "Name" << custName << "\n"
			"Address" << custAddress << "\n" << std::endl;
	}
	void custWelcome(std::ostream &) const
	{
		std::cout << "\nHello " << custName << "\nAddress: " << custAddress << std::endl;
	}
	std::string custValue() const
	{
			return custName;
	}

	Customer(const std::string &inputCustName, const std::string &inputCustAddress)
		: custName(inputCustName), custAddress(inputCustAddress)
	{
	}
};

struct Beverage {
	const std::string name;
	double price;

	void bevInfo(std::ostream &) const
	{
		std::cout << "Name" << name << "\n"
			<< "Price" << "$" << price << "\n"
			<< "Total" << "$" << std::endl;
	}
	void bevSelect(std::ostream &) const
	{
		std::cout << "Selection: " << name << ", price per unit: $" << price << std::endl;
	}
	Beverage(const std::string &bevName, double bevPrice)
		: name(bevName), price(bevPrice)
	{
	}
};


const int AVAILABLE_PRODUCTS_COUNT = 3;
const Beverage AVAILABLE_PRODUCTS[AVAILABLE_PRODUCTS_COUNT] = {
	Beverage("Water", 1.45),
	Beverage("Soda", 2.98),
	Beverage("Ice Tea", 3.29)
};

typedef int Quantities[AVAILABLE_PRODUCTS_COUNT];


double totalCost(Quantities);
void orderSummary(Quantities);
std::string getStringFromUser(const std::string &prompt);
char getCharFromUser(const std::string &prompt);
Customer getCustomerFromUser();
void displayMenu(const Customer &);
void loadProductQuantity(const Beverage &, Quantities);
void doneShowOrder(const Customer &, Quantities);

Customer patName()
{
	std::string custName;
	std::string custAddress;
	std::cout << "Please enter your name ==> ";
	std::getline(std::cin, custName);
	std::cout << "Please enter your address ==> ";
	std::getline(std::cin, custAddress);

	return Customer(custName, custAddress);
}

int main(void) {
	Quantities ordered = { 0 };
	Customer patron = getCustomerFromUser();
	patron.custInfo(std::cout);

	bool done = false;
	while (!done) {
		displayMenu(patron);
		switch (getCharFromUser("Your selection: ")) {
		case '1':
			loadProductQuantity(AVAILABLE_PRODUCTS[0], ordered);
			break;
		case '2':
			loadProductQuantity(AVAILABLE_PRODUCTS[1], ordered);
			break;
		case '3':
			loadProductQuantity(AVAILABLE_PRODUCTS[2], ordered);
			break;
		case 'X': case 'x':
			done = true;
			break;
		default:
			std::cout << "Invalid selection. Please try again";
			std::cin.get();
			// no break in the default case
		}
	}
	doneShowOrder(patron, ordered);

	return 0;
}

void orderSummary(Quantities ordered) {
	std::cout << std::endl;
	std::cout << "======= ORDER SUMMARY ====" << std::endl;
	std::cout << "Items selected" << std::endl;
	for (int i = 0; i < AVAILABLE_PRODUCTS_COUNT; i++) {
		// only show beverages that has at least one order
		if (ordered[i] > 0) {
			// print something for AVAILABLE_PRODUCTS[i] here
		}
	}
}


Now the only thing I see wrong is lines 63-65 have green lines under getStringFromUser, getCharFromUser, getCustomerFromUser. What do I need to do I am assuming create functions but I am not positive and need some direction.
I was able to create two out of three of the functions. I got help with this so I am unsure what to do with the last one and if I even did this properly. I put this in the post also but I really need help I only have 2hours until this is due. I am stressed and unable to think clearly and will definitely look through these tutorials. Just need to get this done. If you can please look at this and help me.

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit

class Customer {
	const std::string custName, custAddress;
public:
	void custInfo(std::ostream &) const
	{
		std::cout << "Name" << custName << "\n"
			"Address" << custAddress << "\n" << std::endl;
	}
	void custWelcome(std::ostream &) const
	{
		std::cout << "\nHello " << custName << "\nAddress: " << custAddress << std::endl;
	}
	std::string custValue() const
	{
		return custName;
	}

	Customer(const std::string &inputCustName, const std::string &inputCustAddress)
		: custName(inputCustName), custAddress(inputCustAddress)
	{
	}
};

struct Beverage {
	const std::string name;
	double price;

	void bevInfo(std::ostream &) const
	{
		std::cout << "Name" << name << "\n"
			<< "Price" << "$" << price << "\n"
			<< "Total" << "$" << std::endl;
	}
	void bevSelect(std::ostream &) const
	{
		std::cout << "Selection: " << name << ", price per unit: $" << price << std::endl;
	}
	Beverage(const std::string &bevName, double bevPrice)
		: name(bevName), price(bevPrice)
	{
	}
};


const int AVAILABLE_PRODUCTS_COUNT = 3;
const Beverage AVAILABLE_PRODUCTS[AVAILABLE_PRODUCTS_COUNT] = {
	Beverage("Water", 1.45),
	Beverage("Soda", 2.98),
	Beverage("Ice Tea", 3.29)
};

typedef int Quantities[AVAILABLE_PRODUCTS_COUNT];


double totalCost(Quantities);
void orderSummary(Quantities);
std::string getStringFromUser(const std::string &prompt);
char getCharFromUser(const std::string &prompt);
Customer getCustomerFromUser();
void displayMenu(const Customer &);
void loadProductQuantity(const Beverage &, Quantities);
void doneShowOrder(const Customer &, Quantities);



void getStringFromUser(std::string &prompt)
{
	
}

void getCharFromUser(std::string &prompt)
{
	std::cout << std::endl << std::endl
		<< &prompt
		<< ", Please select the beverage you would like to purchase from the menu: "
		<< std::endl;

	std::cout << "Drink Menu" << std::endl;
	std::cout << "========" << std::endl;
	std::cout << "1 - Water $1.45" << std::endl;
	std::cout << "2 - Soda $2.98" << std::endl;
	std::cout << "3 - Iced Tea $3.29" << std::endl;
	std::cout << "X - Exit " << std::endl << std::endl;
}


Customer getCustomerFromUser()
{
	std::string custName;
	std::string custAddress;
	std::cout << "Please enter your name ==> ";
	std::getline(std::cin, custName);
	std::cout << "Please enter your address ==> ";
	std::getline(std::cin, custAddress);

	return Customer(custName, custAddress);
}

int main(void) {
	Quantities ordered = { 0 };
	Customer patron = getCustomerFromUser();
	patron.custInfo(std::cout);

	bool done = false;
	while (!done) {
		displayMenu(patron);
		switch (getCharFromUser("Your selection: ")) {
		case '1':
			loadProductQuantity(AVAILABLE_PRODUCTS[0], ordered);
			break;
		case '2':
			loadProductQuantity(AVAILABLE_PRODUCTS[1], ordered);
			break;
		case '3':
			loadProductQuantity(AVAILABLE_PRODUCTS[2], ordered);
			break;
		case 'X': case 'x':
			done = true;
			break;
		default:
			std::cout << "Invalid selection. Please try again";
			std::cin.get();
			// no break in the default case
		}
	}
	doneShowOrder(patron, ordered);

	return 0;
}

void orderSummary(Quantities ordered) {
	std::cout << std::endl;
	std::cout << "======= ORDER SUMMARY ====" << std::endl;
	std::cout << "Items selected" << std::endl;
	for (int i = 0; i < AVAILABLE_PRODUCTS_COUNT; i++) {
		// only show beverages that has at least one order
		if (ordered[i] > 0) {
			std::cout << "You selcted: " << std::endl;// print something for AVAILABLE_PRODUCTS[i]
		}
	}
}


This is the only part I am completely lost on:
1
2
3
4
5
void getStringFromUser(std::string &prompt) //Not sure what should go in this function
{

	
}


Please advise me I am trying to understand here.


Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "void __cdecl displayMenu(class Customer const &)" (?displayMenu@@YAXABVCustomer@@@Z) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "char __cdecl getCharFromUser(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?getCharFromUser@@YADABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "void __cdecl loadProductQuantity(struct Beverage const &,int * const)" (?loadProductQuantity@@YAXABUBeverage@@QAH@Z) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK2019 unresolved external symbol "void __cdecl doneShowOrder(class Customer const &,int * const)" (?doneShowOrder@@YAXABVCustomer@@QAH@Z) referenced in function _main CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\CSS115_IP4_JosephHoffman.obj 1
Error LNK1120 4 unresolved externals CSS115_IP4_JosephHoffman C:\Users\ACEis\source\repos\CSS115_IP4_JosephHoffman\Debug\CSS115_IP4_JosephHoffman.exe 1


These are the errors given by the compiler.
Last edited on
Those are linker errors. They are telling you that you are trying to use several functions that you haven't defined anywhere.

Look at the first error. It's telling you that you're trying to use a function displayMenu(class Customer const &). From your code, I can see you've declared the function at line 66, and you try to call it at line 122, but nowhere in your code have you actually defined that function.

EDIT:

OK, I can see that you know you haven't yet written that function. What is it that you want the function to do? Is it just display the text of the menu? Because you're already doing that inside your getCharFromUser function.

Some of your other linker errors are because you're being inconsistent in how you declare and define your functions:

1
2
3
char getCharFromUser(const std::string &prompt); // Declaration at line 64

void getCharFromUser(std::string &prompt) // Definition at line 77 - see the difference? 


You're getting an error for loadProductQuantity() and doneShowOrder() because you haven't defined those functions yet.

Last edited on
Topic archived. No new replies allowed.