Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;围绕2条stringstreams进行包装(“对话”模仿)_C++_Stl_Stream_Operator Overloading_Wrapper - Fatal编程技术网

C++ C++;围绕2条stringstreams进行包装(“对话”模仿)

C++ C++;围绕2条stringstreams进行包装(“对话”模仿),c++,stl,stream,operator-overloading,wrapper,C++,Stl,Stream,Operator Overloading,Wrapper,我目前正在为两个流编写一个包装器。最后,我想模拟与人的对话,使我的代码清晰易懂,便于使用 声明 class Person { public: void process(); private: std::stringstream _request; std::stringstream _response; } 用法 Person daniel; std::stringstream answer; daniel << "Hello" << std:

我目前正在为两个流编写一个包装器。最后,我想模拟与
人的对话,使我的代码清晰易懂,便于使用

声明

class Person
{
public:
    void process();

private:
    std::stringstream _request;
    std::stringstream _response;
}
用法

Person daniel;
std::stringstream answer;
daniel << "Hello" << std::endl; // f.e std::endl means end of the phrase
daniel << "How" << "are you";
daniel << "doing?";
daniel >> answer;
daniel.process();
std::cout << "Daniel says: " << answer;
// or
std::cout << "Daniel says: " << daniel;
一些例子确实很好,但我绝对乐意看到一些能让我走出僵局的粗略想法。

rdbuf()不返回字符串。您应该调用str()从stringstream中获取std::string。您需要将这两个函数声明为类的朋友,以便访问其私有属性

class Person
{
public:
    void process();

    friend std::ostream& operator<<(std::ostream& o, const person& p);
    friend std::istream& operator>>(std::istream& i, person& p);
private:
    std::stringstream _request;
    std::stringstream _response;
}

std::ostream& operator<<(std::ostream& o, const person& p)
{
    o << p._response.str();

    return (o);
}

std::istream& operator>>(std::istream& i, person& p)
{
    i >> p._request;
    // internal process() call probably will be better
    return i;
}
班级人员
{
公众:
无效过程();
friend std::ostream&operator(std::istream&i,person&p);
私人:
std::stringstream\u请求;
std::stringstream\u响应;
}
std::ostream和操作员>p.\U请求;
//内部进程()调用可能会更好
返回i;
}

老实说,我不明白你为什么要这样做,但我想这是某种练习或家庭作业。无论如何,实现这个功能有一些微妙之处,我想你自己可能无法理解,所以这里是一个可行的解决方案

人 这家伙是来支持std::endl的。请注意,我已经为您的
Person
添加了类似的重载,以便
std::endl
能够正常工作(见下文)。其他重载,如基本类型的重载,留给您作为练习

阅读反应 你一开始想要的那个

int main() {
    Person daniel;

    daniel << "Hello" << std::endl;
    daniel << "How " << "are you";
    daniel << " doing?" << std::endl;

    // We are ready to process the request so do it
    daniel.process();

    // And Daniel's answer is...
    std::cout << "Daniel says: " << daniel << std::endl;

    return 0;
}

“但我被卡住了”问题到底是什么?我想,在理解它应该是怎样的过程中。我觉得自己在这个话题上不舒服。现在,编译器说:person的二进制表达式('person'和'const char[4]')的操作数无效。尽管您所说的部分是正确的(rdbuf
的用法非常好),但最终的解决方案远不能提供所需的功能,因此,一般来说,这个答案是不正确的。更多细节请参见我的答案。
#include <iostream>
#include <sstream>

class Person {
public:
    void process() {
        // Let's see what we've got in `_request`
        std::cout << _request.rdbuf() << std::endl;

        // Do some processing to produce correponding _response

        // In this example we hardcode the response
       _response << "I'm fine, thanks!";
    }

private:
    std::stringstream _request;
    std::stringstream _response;

    // Make them all friends so that they can access `_request` and `_response`
    friend Person& operator<<(Person& p, std::string const& s);
    friend Person& operator<<(Person& p, std::ostream& (*f)(std::ostream&));
    friend std::ostream& operator<<(std::ostream &o, Person& p);
    friend Person& operator>>(Person& p, std::string& s);
};

Person& operator<<(Person& p, const std::string& s) {
    p._request << s;

    return p;
}

// Notice this peculiar signature, it is required to support `std::endl`
Person& operator<<(Person& p, std::ostream& (*f)(std::ostream&)) {
    p._request << f;

    return p;                           
}

// Somewhat conventional stream operator overload (in terms of signature)
std::ostream& operator<<(std::ostream &o, Person& p) {
    o << p._response.rdbuf();

    return o;
}

Person& operator>>(Person& p, std::string& s) {
    // NOTE: This will read not the whole `_reponse` to `s`, but will stop on
    // the first whitespace. This is the default behavior of `>>` operator of
    // `std::stringstream`.
    p._response >> s;

    return p;
}
basic_ostream& operator<<(basic_ostream& st, 
                          std::basic_ostream& (*func)(std::basic_ostream&));
int main() {
    Person daniel;

    daniel << "Hello" << std::endl;
    daniel << "How " << "are you";
    daniel << " doing?" << std::endl;

    // We are ready to process the request so do it
    daniel.process();

    // And Daniel's answer is...
    std::cout << "Daniel says: " << daniel << std::endl;

    return 0;
}
int main() {
    Person daniel;

    daniel << "Hello" << std::endl;
    daniel << "How " << "are you";
    daniel << " doing?" << std::endl;

    // We are ready to process the request so do it
    daniel.process();

    // And Daniel's answer is...
    std::string part1;
    std::string part2;
    std::string part3;

    // Will read "I'm"
    daniel >> part1;

    // Will read "fine,"
    daniel >> part2;

    // Will read "thanks!"
    daniel >> part3;

    std::cout << "Daniel says: " << part1 << " " << part2 << " " << part3 << std::endl;

    return 0;
}