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_data
和 books_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/