C++ 如何将变量传递给类

C++ 如何将变量传递给类,c++,C++,我已经在这个代码上工作了很长一段时间了,之前我已经发布了它,但是在解决了这个问题之后,另一个问题出现了,所以我用这个问题的名称创建了一个新的帖子。好的,问题是我显然没有以正确的方式将变量传递给管理员类。我尝试了两种方法,这是我的书中所展示的,两种方法都给了我一个错误,错误是C2512:“SalariedEmployee”:没有合适的默认构造函数可用 //Lynette Wilkins //Week 12 #include <iostream> #include <cstd

我已经在这个代码上工作了很长一段时间了,之前我已经发布了它,但是在解决了这个问题之后,另一个问题出现了,所以我用这个问题的名称创建了一个新的帖子。好的,问题是我显然没有以正确的方式将变量传递给管理员类。我尝试了两种方法,这是我的书中所展示的,两种方法都给了我一个错误,错误是C2512:“SalariedEmployee”:没有合适的默认构造函数可用

  //Lynette Wilkins
//Week 12

#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

class SalariedEmployee
{
private:
    double wageRate;
    int hours;
protected:
    string name;
    string ssn;
    double netPay;
    string department;

public:
    SalariedEmployee(string n, string s, double np, double w, int h, string d);
     ~SalariedEmployee() {cout<<endl;}
     string Getname();  //returns name
     string Getssn();   // returns social security number
     double GetnetPay(); //returns netPay
     string Getdepartment(); // returns department
     double GetwageRate(); //returns wage rate
     int Gethours(); //returns hours
     void Setname(string); //sets name
     void Setssn(string); //sets ssn
     void SetnetPay(double); //sets net pay
     void Setdepartment(string); //sets department
     void SetwageRate(double); //sets wage rate
     void Sethours(int); //sets hours

};

SalariedEmployee::SalariedEmployee(string n, string s, double np, double w, int h, string d) : name(n),
      ssn(s),
      netPay(np),
      wageRate(w),
      hours(h),
      department(d)
{}


string SalariedEmployee::Getname()
{
    return name;
}

string SalariedEmployee::Getssn()
{
    return ssn;
}

double SalariedEmployee::GetnetPay()
{
    return netPay;
}

double SalariedEmployee::GetwageRate()
{
    return wageRate;
}

int SalariedEmployee::Gethours()
{
    return hours;
}
void SalariedEmployee::Setname(string n)
{
    name = n;

}

void SalariedEmployee::Setssn(string s)
{
    ssn = s;
}

void SalariedEmployee::SetnetPay(double np)
{
    netPay = np;
}

void SalariedEmployee::Setdepartment(string d)
{
    department = d;
}


void SalariedEmployee::SetwageRate(double w)
{
    wageRate = w;
}

void SalariedEmployee::Sethours(int h)
{
    hours = h;
}


class Administrator : public SalariedEmployee
{
protected:
    string title;
    string responsi;
    string super;
    double salary;
public:
    Administrator(string t, string r, string s, double sa);
     ~Administrator();
    string Gettitle();
    string Getresponsi();
    string Getsuper();
    double Getsalary();
    void Settitle(string);
    void Setresponsi(string);
    void Setsuper(string);
    void Setsalary(double);
    void print();
};


Administrator::Administrator(string t, string r, string s, double sa) :  title(t), responsi(r), super(s), salary(sa)
{
}

Administrator::~Administrator()
{
    cout<<endl;
}

string Administrator::Gettitle()
{
    return title;
}

string Administrator::Getresponsi()
{
    return responsi;
}

string Administrator::Getsuper()
{
    return super;
}

double Administrator::Getsalary()
{
    return salary;
}
void Administrator::Settitle(string ti)
{
    title = ti;
}
void Administrator::Setresponsi(string re)
{
    responsi = re;
}


void Administrator::Setsuper(string su)
{
    super=su;
}

void Administrator::Setsalary(double sa)
{
    salary= sa;
}

void Administrator::print( )
  {

    cout << "\n_______________________________________________\n";

    cout << "Pay to the order of " << name<< endl;
    cout << "The sum of " << netPay << " Dollars\n";
    cout << "_________________________________________________\n";
    cout <<endl<<endl;
    cout << "Employee Number: " << ssn << endl;
    cout << "Salaried Employee. Regular Pay: " 
       << salary << endl; 
    cout << "_________________________________________________\n";
  }





int main()
{


    string name;
    string soc;
    double net = 0;
    double wage = 0;
    int hrs = 0;
    string dept;
    string admtitle;
    string resp;
    string sup;
    double sal = 0;
    int response = 0;

    string date = "January 12, 2013";


    cout<<setprecision(2)
    <<setiosflags(ios::fixed)
    <<setiosflags(ios::showpoint);

    SalariedEmployee emp1(name, soc,net, wage, hrs, dept);
    Administrator adm1(admtitle, resp, sup, sal);


while(response != 4){

    cout<<"Employee and Administrator Salary Program "<<endl;
    cout<<"(You will have to enter data first before you do anything else)"<<endl<<endl;
    cout<<"Enter Employee Data,  Enter 1"<<endl;
    cout<<"Change data,   Enter 2"<<endl;
    cout<<"Print Check,   Enter 3"<<endl;
    cout<<"End Program, Enter 4"<<endl<<endl;
    cout<<"Please make your selection"<<endl;



cin>> response;

    switch (response)


    {
    case 1:
        cout <<"The employee's data will be entered here: "<<endl<<endl;

        cout<<"Enter the employees name: ";
        cin.ignore();
        getline(cin, name);

        cout<<"Enter the employees social security number: ";
         cin.ignore();
        getline(cin, soc);

        cout<<"Enter the employees net pay: ";
        cin>>net;

        cout<<"Enter the employees wage rate: ";
        cin>>wage;

        cout<<"Enter the number of hours the employer worked: ";
        cin>>hrs;

        cout<<"Enter the employees title: ";
        cin.ignore();
        getline(cin,admtitle);

        cout<<"Enter the employees area responsibility: ";
        cin.ignore();
        getline(cin, resp);

        cout<<"Enter the employees salary: ";
        cin>>sal;



        cout<<endl<<endl<<endl;


        break;




    case 2:

        cout<<"Please change the data you entered previously here. " <<endl<<endl;

        cout<<"Enter the employees name: ";
        cin.ignore();
        getline(cin, name);

        cout<<"Enter the employees social security number: ";
         cin.ignore();
        getline(cin, soc);

        cout<<"Enter the employees net pay: ";
        cin>>net;

        cout<<"Enter the employees wage rate: ";
        cin>>wage;

        cout<<"Enter the number of hours the employer worked: ";
        cin>>hrs;

        cout<<"Enter the employees title: ";
        cin.ignore();
        getline(cin,admtitle);

        cout<<"Enter the employees area responsibility: ";
        cin.ignore();
        getline(cin, resp);

        cout<<"Enter the employees salary: ";
        cin>>sal;



        cout<<endl<<endl<<endl;
break;



    case 3:

        cout <<"Information Printed"<<endl<<endl;

        cout<<"_____________________________"<<date<<endl;
        &Administrator::print;



        break;


    default:

        cout<<endl<<endl
            <<"Invalid Selection! Try Again"<<endl;
        exit(1);

}
}





    system("PAUSE");
    return 0;
}
//勒奈特·威尔金斯
//第12周
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
阶级受薪雇员
{
私人:
双倍下注率;
整小时;
受保护的:
字符串名;
字符串ssn;
双网支付;
弦乐部;
公众:
受薪员工(字符串n、字符串s、双np、双w、整数h、字符串d);
~SalariedEmployee(){cout
管理员(字符串t、字符串r、字符串s、双sa);
将尝试调用基类的默认构造函数(如果不指定其他构造函数)。(默认构造函数是可以不带任何参数调用的构造函数)

基类没有默认构造函数,因此会出现错误

要调用基类的另一个构造函数,请执行以下操作:

Administrator::Administrator(string t, string r, string s, double sa) :  
SalariedEmployee(<args>), //base constructor call
title(t), responsi(r), super(s), salary(sa) //members
{
}
Administrator::Administrator(字符串t、字符串r、字符串s、双sa):
SalariedEmployee(),//基本构造函数调用
职务(t)、职责(r)、上级(s)、工资(sa)//成员
{
}

OK!!那么你在哪里()这里传递了什么参数?@CarlaBridgesWilkins这是逻辑的一部分。我指出了错误,无法帮助你理解逻辑。想想看。管理员是受薪员工,因此有它的所有属性。当你将其创建为
Administrator adm1时,管理员的名字是什么(admtitle,resp,sup,sal);
?(我猜你必须在
管理员的构造函数中添加更多的参数,以适应基类的参数。好的,我已经尝试过了,但没有任何效果。我想现在就把我的头发拔出来!!!@CarlaBridgesWilkins如果你将构造函数更改为
管理员::管理员该怎么办(字符串t、字符串r、字符串s、双sa、字符串bn、字符串bs、双bnp、双bw、int bh、字符串bd):SalariedEmployee(bn、bs、bnp、bw、bh、bd)、title(t)、responsi(r)、super(s)、salary(sa)
并传递您需要的所有参数。好的,我将尝试一下,看看它是如何工作的。感谢Luchian的帮助