2013-08-12 2 views
0

내 하위 클래스에있는 일부 문자열을 표시하는 데 문제가 있습니다. 함수를 사용하여이 작업을 수행하려고 시도하지만 이러한 문자열의 내용을 왜 얻지 못하는지 확신 할 수 없습니다.C++ 예외 처리 내 자신의 예외 클래스

class Employee{ 
    string FN, LN, JT; 
    double Income; 

public: 
    char const *getters(){ 
     return FN.data(), LN.data(), JT.data(); //=========>getting the content of strings 
    } 
    virtual char const *getAccess()=0; 
    Employee(char const *fn, char const *ln, char const *jt, double inc){ 

     if(fn==0) throw Exception(1, "Sorry, First Name is Null"); 
     if(ln==0) throw Exception(2, "Sorry, Last Name is Null"); 
     if(jt==0) throw Exception(3, "Sorry Job Title is Null"); 
     if(inc<=0) throw Exception(4, "Sorry, The Income is Null"); 

     FN=fn; 
     LN=ln; 
     JT=jt; 
     Income=inc; 
    } 
}; 

class Programmer: public Employee{ 
public: 
    Programmer(char const *fn, char const *ln, double inc): 
     Employee(fn,ln,"Programmer", inc) 
    {} 
    char const *getAccess(){ 
     return "You have access to Meeting Room + Development Office"; 
    } 
}; 

//=========The Main============ 
int main(){ 
    Employee *acc[3]; 

    try{ 
     acc[0]=new Programmer("Juan", "Villalobos", 60000); 
     acc[1]=new Director("Jorge", "Villabuena", 70000); 
     acc[2]=new ProdSupport("Pedro", "Villasmil", 80000); 
     for(int i=0; i<3; i++){ 
      cout << acc[i]->getters() << endl; //=============>Displaying the strings 
      cout << acc[i]->getAccess() << endl; 
     } 
    } catch(Exception acc){ 
     cout << "Err:" << acc.getErrCode() << " Mess:" << acc.getErrMess() << endl; 
    } 

    return 0; 
} 

그래서 내 기능이 내가 원하는 것을 수행하지 않는다고 생각합니다. 이름과 성을 표시합니다. 내가 뭘 잘못하고 있니?

+1

이 그렇게 잘못 내가 아니다 :

이하지만 당신은 아마 원하는 것은

char const *getters(){ return (FN + LN + JT).data(); } 

이 같은 프로그램을 다시 작성합니다입니다

char const *getters(){ return FN.data(), LN.data(), JT.data(); } 

컴파일 않습니다 어디서부터 시작해야할지 ... –

+0

이 경우에는 직책이 JT.d와 동일한 마지막 문자열 만 가져 오는 것을 잊어 버렸습니다. ata() – JV17

+3

당신이하려고하는 것처럼 문자열 목록을 반환 할 수있는 아이디어가 있습니까? 함수에서 리턴 값은 하나뿐입니다. –

답변

1

나는 혼합 점이 없다. char*string. 나중을 선호하십시오.

class Employee{ 
    string FN, LN, JT; 
    double Income; 

public: 
    string getters(){ 
     return FN + " " + LN + " " + JT; 
    } 

    virtual string getAccess()=0; 

    Employee(string const &fn, string const &ln, string const &jt, double inc) : 
     FN(fn), LN(ln), JT(jt), Income(inc) 
    { 
    } 
}; 

class Programmer: public Employee{ 
public: 
    Programmer(string const &fn, string const &ln, double inc): 
     Employee(fn,ln,"Programmer", inc) 
    {} 

    string getAccess(){ 
     return "You have access to Meeting Room + Development Office"; 
    } 
}; 

//=========The Main============ 
int main() 
{ 
    std::vector<Employee> acc; 

    acc.push_back(Programmer("Juan", "Villalobos", 60000)); 
    acc.push_back(Director("Jorge", "Villabuena", 70000)); 
    acc.push_back(ProdSupport("Pedro", "Villasmil", 80000)); 

    for(size_t i=0; i<acc.size(); i++){ 
     cout << acc[i].getters() << endl; 
     cout << acc[i].getAccess() << endl; 
    } 

    return 0; 
} 
+0

나는 한 단계 더 나아가'for' 루프를 인덱스 대신에 반복자를 사용하도록 바꿀 것입니다. –

+0

고마워요. 많은 도움을 주셨습니다. 정말 도움이 돼 주셔서 정말 감사합니다. – JV17

1

, 쉼표 연산자의 결과는 오른쪽 값이므로 return FN.data(), LN.data(), JT.data();은 실제로는 return JT.data();과 동일합니다.

void displayValues() const { 
    cout << FN << " " << LN << " " << JT << endl; 
    cout << getAccess() << endl; 
} 

: 클래스 자체에 cout 논리를 이동,

std::vector<std::string> getValues() const { 
    std::vector<std::string> arr(3); 
    arr.push_back(FN); 
    arr.push_back(LN); 
    arr.push_back(JT); 
    return arr; 
} 

std::vector<std::string> arr = acc[i]->getValues(); 
for (std::vector<std::string>::const_iterator iter = arr.begin(), end = arr.end(); iter != end; ++iter) { 
    cout << *iter << " "; 
} 
cout << endl; 

을 또는 :

당신이하려고하는 일을하려면 대신 시도
for(int i=0; i<3; i++){ 
    acc[i]->displayValues(); 
} 
+0

그래, 정확히 어떻게 작동 시키면 내 함수에서 3 개의 문자열 값을 가져 와서 콘텐츠를 표시하도록 반환 할 수 있습니까? ?? – JV17

+0

업데이트 된 답변보기 –