简体   繁体   English

打印结构成员

[英]Print struct member

#include <iostream>
using namespace std;

struct Person {
    string first_name;
    string last_name;
};

int main() {
    Person a;
    a.first_name = "foo";
    a.last_name = "bar";
    cout << a.first_name << " " << a.last_name;
    return 0;
}

Gives error: 给出错误:

binary '<<' : no operator found which takes a right-hand operand of
type 'std::string' (or there is no acceptable conversion)

Why is it happening? 为什么会这样呢?

As others have mentioned in the comments, you need to include string : 正如其他人在评论中提到的那样,您需要包含string

#include <iostream>
#include <string>
using namespace std;

struct Person {
    string first_name;
    string last_name;
};

int main() {
    Person a;
    a.first_name = "foo";
    a.last_name = "bar";
    cout << a.first_name << " " << a.last_name;
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM