错误不完整元素类型结构

时间:2016-07-02 00:14:44

标签: c++

我在编译代码时遇到问题。每次我编译它我都会得到:

  rec5.c: In function âmainâ:
  rec5.c:7:17: error: array type has incomplete element type
struct Item cart[3];
             ^
  rec5.c:8:16: error: array type has incomplete element type
struct Item book[4];
            ^
  rec5.c:9:16: error: array type has incomplete element type
struct Item clothing[5];
            ^
  rec5.c:10:16: error: array type has incomplete element type
struct Item sports[6];
            ^
  rec5.c:20:5: error: expected â;â before âprintfâ
printf("%s %d %d", book.name, book.price, book.quantity);
^
  rec5.c:24:6: error: expected â;â before âprintfâ
printf("%s %d %d", clothing.name, clothing.price, clothing.quantity);
^
  rec5.c:28:6: error: expected â;â before âprintfâ
printf("%s %d %d", sports.name, sports.price, sports.quantity);

我尝试在struct之前使用typedef但是我得到了“未声明的数组大小”。我不知道为什么没有正确宣布物品。该功能应该询问客户他们想要哪个项目,然后显示正确的数据。

#include <stdio.h>

int main(){
    struct Item cart[3];
    struct Item book[4];
    struct Item clothing[5];
    struct Item sports[6];

    book.name = "harry potter";
    book.price == "$100";
    clothing.name = "shirt";
    clothing.price == "$15";
    sports.name = "football";
    sports.price = "20";

    scanf("enter Item %c", cart.type);

    if (cart.type == "book"){
        scanf("please enter quantity %d", book.quantity)
        printf("%s %d %d", book.name, book.price, book.quantity);
    }
    if (cart.type == "clothing"){
        scanf("please enter quantity %d", clothing.quantity)
        printf("%s %d %d", clothing.name, clothing.price, clothing.quantity);
    }
    if (cart.type == "sports"){
        scanf("please enter quantity %d",sports.quantity)
        printf("%s %d %d", sports.name, sports.price, sports.quantity);
    }
}

struct Item
{
    char *type;
    char *name;
    double price;
    double quantity;
};

1 个答案:

答案 0 :(得分:3)

您的struct在被定义之前正在使用。只需将struct定义移至main()函数之上即可。