c++ - 数据结构新手

标签 c++ string data-structures c++11

c++ primer 第 5 版的练习题要求编写一个自己的 sales_data 类版本。

我是这样做的

#include <iostream>
#include <string>

struct sales data
{
    string bookno;
    unsigned int books sold;
    double revenue;
};

int main()
{
    return 0;
}

运行它会出现以下错误:

Variable sales_data has an initializer, but an incomplete type

String was not declared in this scope (How do I declare a string?)

最佳答案

第一个问题:您忘记了名称 sales_databooks_sold 中的下划线(或其他字符)。 C++ 中的标识符不能包含空格:

struct sales_data
//          ^

unsigned int books_sold;
//                ^

第二个问题:你应该用它所属的命名空间限定string:

    std::string bookno;
//  ^^^^^

或者在使用非限定的 string 名称之前对其进行 using 声明:

using std::string;

这就是您的程序经过上述所有修复后的样子:

#include <iostream> // You don't seem to need this for this program
#include <string>

struct sales_data
{
    std::string bookno;
    unsigned int books_sold;
    double revenue;
};

int main()
{
    return 0;
}

关于c++ - 数据结构新手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16893452/

相关文章:

c++ - 从结构中引用对象的指针

algorithm - 选择 R* 树分割平面

python - 我怎样才能提高这个最短路径/捷径(数组图DS)解决方案的速度?

c++ - QWidget 不显示 QLabel

c++ - vector push_back 非常慢

c++ - 在 Enter 键处拆​​分字符串

c++ - 设计一个结合了同步和异步操作的 C++ API

c# - `\` 的字符串替换没有达到我的预期。是什么赋予了?

java - String[]a 和 String...a 的区别

java - 如何存储字符串的不同字符