【问题标题】:C++ Returning a string through an accessorC ++通过访问器返回字符串
【发布时间】:2015-10-04 15:38:48
【问题描述】:

我正在尝试使用访问器通过 main() 将信息从我的类输出到控制台。但是,我不确定如何在没有返回值或包含我的输出的返回值的情况下返回我的输出。任何帮助将不胜感激..

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;


}

【问题讨论】:

    标签: c++ string class accessor


    【解决方案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'的字符串

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 2015-01-13
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多