无法实例化类

时间:2012-11-20 19:52:49

标签: c++ class instantiation

我看不出我在这里做错了什么。我不打算完全展示我的主要功能,因为我认为这不重要。

我的问题与我创建的这个类有关:

class employee
{
    //create private variables for divider
    string firstName;
    string lastName;
    char gender;
    int dependants;
    double annualSalary;
    static int numEmployees;

public:

    Benefit1 benefit;

    employee()
    {
        //create default values for varaibles
        firstName = "not given";
        lastName = "not given";
        gender = 'U';
        dependants = 0;
        annualSalary = 2000;
    }

    employee(string first, string last, char gen, int dep, double salary, Benefit1 ben)
    {
        //allow input
        firstName = first;
        lastName = last;
        gender = gen;
        dependants = dep;
        annualSalary = salary;
        benefit = ben;
    }
}

(是的,在课堂上正确调用了Benefit1。)当我尝试将其实例化为employee2时,我的问题出现了:

employee employee2("Mary", "Noia", 'F', "5", 24000.0, benefit1);

出于某种原因,我的程序不允许我在第一个实例中使用“Mary”这个词。正如你所看到的那样,第一个实例假设是字符串优先,那么为什么不让它使用呢?

2 个答案:

答案 0 :(得分:4)

问题在于第五个参数 - 它期望int并且你通过它"5"。尝试:

employee employee2("Mary", "Noia", 'F', 5, 24000.0, benefit1);

答案 1 :(得分:0)

您传递的第四个参数必须是int: -

employee employee2("Mary", "Noia", 'F', 5, 24000.0, benefit1);