2015-10-04 38 views
1

我想通過main()來使用訪問器將我的類的信息輸出到控制檯。但是,我不確定如何返回我的輸出而沒有返回值或包含我的輸出的返回值。任何幫助,將不勝感激..C++通過訪問器返回字符串

string WorkTicket::showWorkTicket(int ticketNumber, string clientID, int day, int month, int year, string description) const 
{ 


system("cls"); 
cout << setw(10) << "Ticket # : " << ticketNumber << endl; 
cout << setw(10) << "Client ID : " << clientID << endl; 
cout << setw(8) << "Date : "<< day << "/" << month << "/" << year << endl; 
cout << setw(10) << "Description : " << description << endl; 


} 

回答

1

您可以使用一個字符串流像std::ostringstream,而不是std::cout建立你的內存字符串而不是打印出來的。例如:

std::string make_string_from_stuff(int x, float y, const std::string& name) { 
    std::ostringstream oss; 
    oss << "[" << x << ", " << y << "] : '" << name << "'"; 
    return oss.str(); 
} 

將建立像[1, 2.5] : 'Joe'

字符串