2017-10-10 2 views
-6

개요 : 여러 클래스 (정확히 4 개)가있는 은행 계좌 프로그램을 만들려고합니다. 여기에 계층 구조입니다 -여러 클래스의 개인 데이터 멤버를 설정하고 호출하는 방법은 무엇입니까?

은행

계정 계정;

계정

예금자의 depositor_info을;

Int Account_number;

Double account_balance;

예금자

이름 depositor_name;

문자열 사회 보장 번호;

이름

문자열, 성;

저는 예금주의 이름을 설정하고 예금주를 계좌에 지정할 수 있습니다. 그러나 나는 예금자의 이름을 출력하는 것처럼 보이지 않습니다. 주요 테스트 코드는 다음과 같습니다.

Account test[MAX_ACCTS]; 

string first = "john", last = "doe", social = "132456789"; 
int acctNumber = 987654; 
Name name; 
Depositor depositor; 

name.setFirst(first); // works 
name.setLast(last); // works 

depositor.setName(name); // this works 
depositor.setSSN(social); // this works 

test[1].setDepositor(depositor); // this also works. 

cout << test[1].getDepositor(); // Here I get an error: 
no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Depositor') 

무엇이 잘못 되었나요?

+2

[mcve] .and [ask] questions –

+1

에 대해 읽으십시오. 그런데 오류 메시지를 읽었습니까? 그 문장에서 어떤 단어를 이해하지 못했습니까? –

+1

당신은 C++에게'Depositor'을 출력하는 법을 말하지 않았습니다. 'cout << test [1] .getDepositor(). depositor_name;'을 사용 했습니까? 또는 예 [here] (https://stackoverflow.com/questions/19167404/operator-overloading-ostream-istream)에 설명 된대로 [Depositor]를 인쇄 할 수있는 함수를 추가 할 수 있습니다. – nwp

답변

2

std::ostream&Depositor const&에 대해 사용자 정의 operator<< 오버로드를 인수로 정의해야합니다. C++은 객체를 텍스트로 변환하는 방법을 암묵적으로 알고 있지 않습니다.

std::ostream & operator<<(std::ostream & out, Depositor const& depositor) { 
    out << depositor.getName().getLast() << ", " << depositor.getName().getFirst(); 
    out << "; " << depositor.getSSN(); 
    return out; 
} 

분명히 이름 + SSN을 인쇄하는 것이 원하는 동작이 아니라면 특정 동작을 조정할 수 있습니다.

관련 문제