有关下面 C++ 代码的说法,错误的是( )。
#include <iostream> #include <string> using namespace std; class MyStr { string data; public: MyStr(string _data): data(_data) {} }; int main() { MyStr st("ABC"); cout << st << endl; return 0; }
代码 cout << st << endl; 会报错,因为没有为 MyStr 类重载 << 运算符。
cout << st << endl;
MyStr
<<
第 6 行代码的 data 是 MyStr 类的成员变量。
data
代码 MyStr st("ABC"); 不会报错,将执行构造函数。
MyStr st("ABC");
代码 cout<< st.data <<endl; 可输出 ABC
cout<< st.data <<endl;
ABC