提问者:小点点

在C++代码中遇到编译错误的问题


我发现编译错误。其实很管用。但我把这个代码提交给了网站进行分配。一个网站警告我,您有一个问题,说“不匹配'operator='(操作数类型是'product'和'')”我该如何解决这个错误呢?我应该在哪里修复代码?

#include <iostream>
using namespace std;

struct Product {
    char name[3][6];
    int price[3];
};

int main() {

    Product A, B, C;
    char ch;
    int sum = 0;
    int average;
    
    A = { "strawberry", "melon", "watermelon", 300, 500, 1000 };
    B = { "strawberry", "melon", "watermelon", 450, 450, 900 };
    C = { "strawberry", "melon", "watermelon", 200, 150, 700 };

    cin >> ch;

    if (ch == 'A') {
        for (int i = 0; i < 3; i++) {
            sum += A.price[i];
        }
    }
    else if (ch == 'B') {
        for (int i = 0; i < 3; i++) {
            sum += B.price[i];
        }
    }
    else {
        for (int i = 0; i < 3; i++) {
            sum += C.price[i];
        }
    }

    average = sum / 3;

    cout << average;

    return 0;
}

共1个答案

匿名用户

结构的初始化无效。您必须在其声明时初始化。

  Product  A = { {"strawberry", "melon", "watermelon"}, {300, 500, 1000}};
  Product  B = { {"strawberry", "melon", "watermelon"}, {450, 450, 900 }};
  Product  C = { {"strawberry", "melon", "watermelon"}, {200, 150, 700 }};

并且应增加名称的列大小。
字符名称[3][6];更改为字符名称[3][20];

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|代码|中|编译)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?