Using classes and friends of classes

Sorry - deleted post for academic reasons, don't want teacher to think I copied my own program, lol
Last edited on
Line 57: You invoke Customer using the default constructor, but you have no default constructor for Customer.

Line 60: You invoke City using the default constructor, but you have no default construuctor for City.
What is line 17 doing then? I thought I was creating a default constructor for Customer. Same for line 37, is that not creating a default constructor?

Can you give an example of what I need to do?

Thanks for the help
The default constructor is the constructor that can be called with 0 parameters.

Your Customer constructor on line 17 requires 2 parameters.
Your City constructor on line 37 requires 3 parameters.

Therefore neither of these are default constructors.

Though you do not need a default constructor, really. You just have to call the parameterized constructors:

1
2
3
4
5
6
7
8
// This code is wrong:
Customer aCust;  // <- this calls the default constructor
aCust.Customer(1572, "77777"); // <- this attempts to call a different constructor
   // after aCust has already been default constructed (ie:  this is wrong)


// You want to do this:
Customer aCust(1572, "77777"); // <- use parameterized constructor 
THATS what was wrong. Thank you so much! He never showed us that you can initialize an object and use parameters to pass the data right from the object when you create it which was creating this whole mess to begin when you don't have/need a default constructor.

I was so confused but it makes a lot of sense now.

Awesome! Thanks again for the help you two! You saved me a lot of frustration.
Alright, I cleaned up the code and ran into a NEW issue: I can't get aTown.city or aTown.state to cout properly, however aCust has no issues couting the customer data 1572 and 77777

I fixed line 31 as well to:

friend void displayCust(Customer aCust, City aTown);

I changed main to:

1
2
3
4
5
	Customer aCust(1572, "77777");
	City aTown("Lucky", "Texas", "77777");
	
	displayCust(aCust, aTown);
	


going back to displayCust function, I have

1
2
3
4
5
6
void displayCust(Customer aCust, City aTown)
{
	cout << "Customer No.	City	State	Zipcode" << endl;
	cout << aCust.custNum << "		" << aTown.city << " 	" << aTown.state << aCust.zipCode;
	cout << "\n\n\n\n";
}



Any ideas why aCust couts properly, yet aTown does not? The code is the same from what I can tell.
1
2
3
4
5
6
7
City::City(string ct, string st, string zip)
{
	string city = ct;
	string state = st;
	string zipCode = zip;
	
}


You are not assigning the class members here. You are creating new strings with the same name and assigning them.

As a result, City's members remain empty.

To fix, remove the 'string' so you do not create a new variable:

1
2
3
4
5
6
7
City::City(string ct, string st, string zip)
{
	city = ct;
	state = st;
	zipCode = zip;
	
}
I actually had just caught the error myself after posting and looking at my other function. You are spot on! Program works 100% as intended now. Thank you for the help.

I accidently put in there just by force of habit, I need to stop doing that.

Once again, thanks!
Topic archived. No new replies allowed.