【发布时间】:2016-11-20 08:07:05
【问题描述】:
我注意到我设法编译了以下代码,但不包含 <string>。它不应该给我一个错误吗?
#include <iostream>
using namespace std;
struct Sales_data {
string book_no;
double units_sold;
double price;
};
int main() {
Sales_data book1, book2;
cout << "Please enter first transaction in format: ISBN, units sold & price" << endl;
cin >> book1.book_no >> book1.units_sold >> book1.price;
cout << "Please enter second transaction in format: ISBN, units sold & price" << endl;
cin >> book2.book_no >> book2.units_sold >> book2.price;
cout << "******************************" << endl;
cout << "Total units sold = " << book1.units_sold + book2.units_sold << endl;
cout << "Total revenue = " << (book1.units_sold * book1.price) + (book2.units_sold * book2.price) << endl;
return 0;
}
编译结果:
[yapkm01][~/C++Primer/chapter2]# g++ -std=c++11 -o 2-41a 2-41a.cc
[yapkm01][~/C++Primer/chapter2]#
【问题讨论】:
-
iostream显然在您的标准库实现中包含string。这是偶然的——包括你知道你需要的东西。 -
"[yapkm01][~/C++Primer/chapter2]# g++ " - 为什么要以 root 身份编译代码?没关系(很多),只是好奇..
-
@JesperJuhl 不,我没有以 root 身份编译。我正在使用我的 id "yapkm01" 进行编译。
-
@yapkm01 问题中的井号 (#) 提示另有说明;-) "#" 用于 root 用户,普通用户通常会得到 "$"。 "[yapkm01][~/C++Primer/chapter2]# g++ ..."