我想实现一个成员匹配这个函数的类:
int get(
string &host_,
string &port_,
string url_path,
ostream &out_,
vector<string> &headers,
unsigned int timeout
)
我有这个:
#include <string>
#include <vector>
#include <iostream>
#include <istream>
#include <ostream>
#include "Register.h"
using namespace std;
class Request : public Register {
private:
string *host;
string *port;
string *url;
ostream *out;
vector<string> *header;
unsigned int *timeout;
public:
Request() {
this -> host = new string();
this -> port = new string();
this -> url = new string();
this -> out = new ostream();
this -> header = new vector<string>();
this -> timeout = new int();
}
但我无法实例化它。例如,ostream 是怎么回事:
this -> out = new ostream();
我对 c++ 还是个新手,现在我完全糊涂了,我在谷歌上找不到正确的答案。
最佳答案
std::ostream
是特定流实现的基类,例如 std::ofstream
,你不能直接实例化它。
在 C++ 中,与 Java 不同,我们不会在堆上分配所有内容,而是更喜欢值语义:
/// Can be easily copied
class HoldSomething {
private:
std::string name;
std::vector<std::string> values;
public:
/// constructors
HoldSomething() : name(), values() {}
explicit HoldSomething( const std::string& name ) :
name( name ), values() {}
/// modifiers
void addValue( const std::string& val ) {
value.push_back( val );
}
...
关于具有 asio get 方法成员的 C++ 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11902437/