Using two constructors on one object

73 Views Asked by At

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?

2

There are 2 best solutions below

0
Remy Lebeau On

You are trying to call a non-existent operator() on an existing User object, 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:

    User newUser2(Income, Savings, Debt);
    
  • Otherwise, if your intent is to modify an existing object, then you will have to add additional methods to handle that task, eg:

    class User
    {
    public:
        int UserAge;
        double netIncome, totalSavings, totalDebt;
        string UserName;
    
        User()
        {
            setUserInfo("", 0);
            setMoneyInfo(0, 0, 0);
        }
    
        //Constructor for name and age
        User(string name, int age)
        {
            setUserInfo(name, age);
            setMoneyInfo(0, 0, 0);
        }
    
        //Constructor for money info
        User(double income, double savings, double debt)
        {
            setUserInfo("", 0);
            setMoneyInfo(income, savings, debt);
        }
    
        void setUserInfo(string name, int age)
        {
            UserName = name;
            UserAge = age;
        }
    
        void setMoneyInfo(double income, double savings, double debt)
        {
            netIncome = income;
            totalSavings = savings;
            totalDebt = debt;
        }
    };
    

    Then you can do things like this:

    User newUser(Income, Savings, Debt);
    ...
    newUser.setUserInfo(Name, Age);
    
    User newUser(Name, Age);
    ...
    newUser.setMoneyInfo(Income, Savings, Debt);
    
    User newUser;
    ...
    newUser.setUserInfo(Name, Age);
    newUser.setMoneyInfo(Income, Savings, Debt);
    
0
JaMiT On

Am I not supposed to use two constructors on one object?

Correct.

Picture this. You are in charge of a construction crew. You are sent to an empty field to build a house. You go with your crew, build the house, and return home. Fully routine. Shortly after that, you are sent to the same field -- no longer empty -- to build a house. In the same location as the house that is currently there. How would you do that?

You cannot. Not unless you destroy the existing house first. Same thing with objects. Once constructed, an object cannot be re-constructed unless it is destroyed first. You can replace an object (e.g. using assignment), but not re-construct it.

If you need to construct your object in stages, use a constructor plus a setter. If you don't have a setter, then you cannot contruct your object in stages. Instead, you would need to keep the data in separate variables until you can call a constructor with all the data an object is supposed to hold.

User newUser(Name, Age);
newUser.setIncome(Income);
newUser.setSavings(Savings);
newUser.setDebt(Debt);
// Or a combo setter like
newUser.setFinances(Income, Savings, Debt);

Or

User newUser(Name, Age, Income, Savings, Debt);

If your intent is really to replace the existing object with a new one (losing the name and age information), you could define an assignment operator and assign a new object to the old. This would call the constructor a second time, but the second time is for a new (unnamed, temporary) object. After the assignment, the new object is destroyed, but a copy (or move) of its data is retained by the original object. The end result is similar to destroying the original object and constructing a replacement in the same place.

User newUser(Name, Age);
newUser = User(Income, Savings, Debt); // Name and Age are lost

Caveat: This assumes the traditional semantics for assignment, which is not enforced by the language.