2013-01-10 3 views
-2

실제로 프로그램에서 case 문에있는 변수가 있습니다. 나는 그들에게 내 수업 기능을 갖도록 노력했지만 오류가 계속 발생한다. SalariedEmployee와 Administrator 클래스로 갈 변수를 얻고 싶습니다.클래스 메서드에 변수를 전송하는 방법

//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 

}; 


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() 
{ 
    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; 

    string date = "January 12, 2013"; 


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

    SalariedEmployee emp1(name, soc,net, wage, hrs, dept); 


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; 
} 
+0

어떤 "오류"가 발생하고 있습니까? – phonetagger

+0

main.obj : 오류 LNK2019 : 해결되지 않은 외부 기호 "public : __thiscall SalariedEmployee :: SalariedEmployee (class std :: basic_string , 클래스 std :: allocator >, 클래스 std :: basic_string , 클래스 std :: allocator >, double, double, int, 클래스 std :: basic_string , 클래스 std :: allocator >) "?? 0SalariedEmployee @ _ QAE @ V? $ basic_text @ D @ std @@ V? _ allocator @ D @ 2 std @@ 0NNH0 @ Z) 함수 _main에서 참조되는 1> 치명적인 오류 LNK1120 : 해결되지 않은 1 개의 외부 –

+0

해당 링크 오류는 코드가 SalariedEmployee :: SalariedEmployee() 메서드 (즉 생성자)를 정의하지 않음을 나타냅니다. – phonetagger

답변

1

당신은 SalariedEmployee의 생성자를 구현하지 않았습니다.

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) 
{ 
} 
+0

OOOOOH !!!다시 프로그램으로 돌아가 보자. –

2

SalariedEmployee 또는 Administrator의 생성자는 절대로 정의하지 않았습니다. 또 다른 응답자는 기존 정의의 서명과 일치하는 생성자 구현을 정의하는 방법을 보여 주었지만 emp1 객체를 인스턴스화 할 때 코드의 변수 값은 의미가 없으므로 사용하는 것이 좋습니다. 당신은 단순히 0으로 대부분의 변수를 초기화 기본 생성자 : 나는 string 멤버를 초기화 귀찮게하지 않았다

SalariedEmployee::SalariedEmployee() : 
    wageRate(0), hours(0), netPay(0) {} 

참고; 자동으로 ""(아무 것도) 초기화하지 않습니다.

또한 getline()으로 데이터를 입력하면 아무 것도하지 않습니다. 세터 함수 중 하나를 호출하여 getline(cin,...)에서 읽은 값을 emp1 객체로 전달합니다. '3'옵션은 이전에 입력 한 내용을 인쇄하는 것처럼 보이지만 인쇄 기능은 사용하지 않습니다. 코드에 &Administrator::print;이 있지만 아무 것도 인쇄되지 않습니다. 이 문은 Administrator 클래스의 print 메서드 주소로 평가되지만 해당 주소는 아무 것도 수행하지 않으므로이 문은 아무 것도 수행하지 않습니다. emp1.print()으로 전화 할 수도 있지만 emp1SalariedEmployee이 아닌 Administrator이 아니며 SalariedEmployee 클래스에는 print() 메서드가 없습니다.

클래스에서 가상 상속 (다형성)에 대해 이야기 했습니까? 그렇다면 SalariedEmployee 클래스에서 print() 메서드를 선언 한 다음 Administrator에이 메서드의 구현을 정의해야합니다. class SalariedEmployee에 그래서, 당신이 원하는 것 :

void print() = 0; 

그런 다음, class Administrator, 당신이 한 단지로 정의한다. 그러나 SalariedEmployee에서 상속 된 유형의 객체가 print() 메소드를 가져야한다고 선언 했으므로 SalariedEmployee이 단지 추상 기본 클래스이기 때문에 emp1 객체를 만들 때는 Administrator이어야합니다. print() isn ' 실제로는 SalariedEmployee 클래스에서 정의 됨).

+0

잘 보시라. 내가 여기에있는 데이터를 사용자에게 묻자 생각했기 때문에 혼란 스럽다. - \t SalariedEmployee emp1 (name, soc, net, wage, hrs, dept); -> 생성자가 정의되었습니다. –

+1

@CarlaBridgesWilkins, 선언되었지만 정의되지 않았습니다. 어쨌든, 어떤 일이 일어날 지 생각하기 위해서, 클래스는 데이터 멤버로서 참조를 저장해야합니다. 이는 매우 나쁜 해결책입니다. 대신 생성자는 일반적으로 클래스에 데이터를 복사하거나 실제로 복사하지 않고 멤버를 설정합니다 (예 :'std :: vector'는 크기를 취하고 그 데이터를 설정하여 값으로 초기화 된 많은 요소를 갖습니다).). – chris

+1

@CarlaBridgesWilkins - 사용자에게 데이터를 요청하는 코드에는 사용자의 응답을'emp1' 객체에 넣으라는 지시가 없습니다. 아아! 나는 당신의 오해가 어디 있는지 봅니다 : 생성자 호출은'name','soc','net' 등의 변수를 모든 영원한'emp1' 객체에 묶지 않습니다; 변수의 * current * 값을 객체의 생성자에 전달하기 만하면됩니다. 이러한 값이 나중에 변경되면, 그 변경 사항은'emp1' 객체로 전달되지 않습니다. 예를 들어,'emp1.Setname (name);을 호출하여 그들 자신을 전달해야합니다. – phonetagger

관련 문제