簡體   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