I have a User class that has two constructors. When I create an object and use the two constructors, one of them gives me an error saying: no match for call to '(User) (double&, double&, double&)'
class User
{
public:
int UserAge;
double netIncome, totalSavings, totalDebt;
string UserName;
//Constructor for name and age
User(string name, int age)
{
UserName = name;
UserAge = age;
}
//Constructor for money info
User(double income, double savings, double debt)
{
netIncome = income;
totalSavings = savings;
totalDebt = debt;
}
};
Main:
int main()
{
string Name, answer;
int Age;
double Income, Savings, Debt, Cost, goalCount;
cout << setw(82) << "--------------------------------------------------------------" << endl;
cout << setw(75) << "Hello and welcome to the RS Money Management App." << endl << endl;
cout << setw(76) << "Designed to help you be responsible with your money!" << endl;
cout << setw(82) << "--------------------------------------------------------------" << endl << endl;
cout << setw(45) << "Please Enter Your Name: "; cin >> Name;
cout << endl;
cout << setw(44) << "Please Enter Your Age: "; cin >> Age;
User newUser(Name, Age); //object created
cout << endl;
system ("CLS");
cout << "------------------------------" << endl;
cout << setw(15) << "Welcome, " << newUser.UserName << "." << endl;
cout << "------------------------------" << endl;
cout << "Let's start by asking you some simple questions." << endl << endl;
Goals financialGoals[10];
cout << "What is your current monthly net Income? "; cin >> Income;
cout << "How much are you currently saving? "; cin >> Savings;
cout << "Do you have Debts? "; cin >> answer;
if (answer == "yes")
{
cout << "What amount of debt must you pay? "; cin >> Debt;
}
else if (answer == "no")
{
cout << "Great." << endl << endl;
Debt = 0;
}
newUser(Income, Savings, Debt); //2nd constructor, where error is occuring
I am not sure what I am doing wrong. Am I not supposed to use two constructors on one object? Am I missing something?
You are trying to call a non-existent
operator()on an existingUserobject, expecting it to call the 2nd constructor. That is not how constructors work. They can only be used to create new objects, not modify objects.So, you need to either:
create a new, separate object, eg:
Otherwise, if your intent is to modify an existing object, then you will have to add additional methods to handle that task, eg:
Then you can do things like this: