打印结构成员

时间:2015-10-31 18:35:20

标签: c++

#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;
}

给出错误:

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

为什么会这样?

1 个答案:

答案 0 :(得分:0)

正如其他人在评论中提到的那样,您需要加入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;
}