繁体   English   中英

数据结构新手

[英]New to data structures

C ++入门第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;
}

这个

struct sales data

应该

struct sales_data

注意下划线。 标识符或类型名称中的空格不合法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM