Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.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++ ostream方法不起作用_C++_Methods_Iostream - Fatal编程技术网

C++ ostream方法不起作用

C++ ostream方法不起作用,c++,methods,iostream,C++,Methods,Iostream,我有两个方法,第一个是定义的字符串: 我不明白为什么它不能成功,以及我如何从main调用这个方法 Actor::operator std::string( ) const { std::stringstream ss; ss << this->_id; std::string str1 = ss.str(); std::stringstream s; s << this->_salary; std::strin

我有两个方法,第一个是定义的字符串:

我不明白为什么它不能成功,以及我如何从main调用这个方法

Actor::operator std::string( ) const {
    std::stringstream ss;
    ss << this->_id;
    std::string str1 = ss.str();

    std::stringstream s;
    s << this->_salary;
    std::string str2 = s.str();

    std::string str3 = "Actor first name = " + this->_firstname + ", last name = " + this->_lastname+", id = " + str1 + ", monthly salary = " + str2;
    if (this->_hasoscar==true)
        str3+=" was NOMINATED Oscar AWARD..";

    return str3;
}
下一个需要打印出来

const Actor& Actor::print(std::ostream& os) {
    os<< std::string();
    return *this;
}

不清楚为什么要这样做,因为正常的工作方式是重载ostream运算符:

class Actor  {
public:
  friend std::ostream& operator<< (std::ostream& os, const Actor& a) {
    os << "Actor first name = " + a._firstname + 
          ", last name = " + a._lastname+", id = " + 
          a._id + ", monthly salary = " + a._salary;
    if (this->_hasoscar) {
       os << " was NOMINATED Oscar AWARD.."; 
    } 
    return os;
  }
};
允许从参与者隐式转换为字符串,例如:

Person a;
std::string s = a;

你误用了,不要编译?问题出在哪里?它是编译的,但我不知道如何从main@GR这有什么关系吗@詹姆斯:这对我很重要。我不得不忍受,他们也要忍受!我不是这样称呼它的:a.print?@user3669000:不,这不是传统的做法。不过,这也不会错。如果像id和薪水这样的东西不是原始代码所指示的字符串(似乎使用stringstream来转换它们),可能需要将其拆分一下。
Actor::operator std::string() const();
Person a;
std::string s = a;
os << "";
os << static_cast<std::string>(*this);
os << std::string(*this);

os << (std::string)*this;

std::string s = *this;
os << s;
os << this->operator std::string();
Actor::operator std::string( ) const {
    std::stringstream ss;
    ss << "Actor first name = " << this->_firstname
       << ", last name = " << this->_lastname
       << ", id = " << this->_id
       << ", monthly salary = " << this->_salary;

    if (this->_hasoscar==true)
        ss << " was NOMINATED Oscar AWARD..";

    return ss.str();
}