C++ 访问和修改函数中的结构数组

C++ 访问和修改函数中的结构数组,c++,C++,我无法通过函数访问和修改结构数组。我目前收到一个错误“表达式必须是指向完整类型的指针”。我将感谢任何帮助 #include <iostream> #include <iomanip> using namespace std; const int NUM_PRODS = 9; int main() { int id[NUM_PRODS] = {914, 915, 916, 917, 918, 919, 920, 92

我无法通过函数访问和修改结构数组。我目前收到一个错误“表达式必须是指向完整类型的指针”。我将感谢任何帮助

#include <iostream>
#include <iomanip>
using namespace std;

const int NUM_PRODS = 9;

int main()
{
   int id[NUM_PRODS] = {914, 915, 916, 917, 918, 919, 920,
                        921, 922};

   int units[NUM_PRODS] = {842, 416, 127, 514, 437, 269, 97,
                           492, 212};

   double prices[NUM_PRODS] = {12.95, 14.95, 18.95, 16.95, 21.95,
                               31.95, 14.95, 14.95, 16.95};
    
   struct Products {
        int id;
        int units_sold;
        double price;
        double sales;
    };

    Products product_list [NUM_PRODS];

    for (int x = 0 ; x < NUM_PRODS; x++) {
        product_list[x].id = id[x];
        product_list[x].units_sold = units[x];
        product_list[x].price = prices[x];
    }    

   calcSales(NUM_PRODS, product_list);

   return 0;
}

void calcSales(const int NUM_PRODS, struct Products product_list[9])
{
   for (int x = 0 ; x < NUM_PRODS; x++) {
      product_list[x]->sales = product_list[x]->units_sold * product_list[x]->price;
    }
}
#包括
#包括
使用名称空间std;
const int NUM_PRODS=9;
int main()
{
int id[NUM_PRODS]={914,915,916,917,918,919,920,
921, 922};
整数单位[NUM_PRODS]={842416127514437269297,
492, 212};
双倍价格[NUM_PRODS]={12.95,14.95,18.95,16.95,21.95,
31.95, 14.95, 14.95, 16.95};
结构产品{
int-id;
已售出的国际单位;
双倍价格;
双重销售;
};
产品列表[产品数量];
对于(int x=0;x销售=产品清单[x]->售出单位*产品清单[x]->价格;
}
}

非常感谢cigien的回答,为了修复“表达式必须是指向完整类型的指针”,结构必须在main之外;此外,应将->操作数替换为“.”,以便访问数组的结构元素以创建类似以下内容的完成代码

#include <iostream>
#include <iomanip>
using namespace std;

const int NUM_PRODS = 9;

struct Products {
   int id;
   int units_sold;
   double price;
   double sales;
};

int main()
{
   int id[NUM_PRODS] = {914, 915, 916, 917, 918, 919, 920,
                        921, 922};

   int units[NUM_PRODS] = {842, 416, 127, 514, 437, 269, 97,
                           492, 212};

   double prices[NUM_PRODS] = {12.95, 14.95, 18.95, 16.95, 21.95,
                               31.95, 14.95, 14.95, 16.95};
    

Products product_list [NUM_PRODS];

for (int x = 0 ; x < NUM_PRODS; x++) {
    product_list[x].id = id[x];
    product_list[x].units_sold = units[x];
    product_list[x].price = prices[x];
}    

calcSales(NUM_PRODS, product_list);

return 0;
}

void calcSales(const int NUM_PRODS, struct Products product_list[])
{
   for (int x = 0 ; x < NUM_PRODS; x++) {
      product_list[x].sales = product_list[x].units_sold * product_list[x].price;
  

  }
}
#包括
#包括
使用名称空间std;
const int NUM_PRODS=9;
结构产品{
int-id;
已售出的国际单位;
双倍价格;
双重销售;
};
int main()
{
int id[NUM_PRODS]={914,915,916,917,918,919,920,
921, 922};
整数单位[NUM_PRODS]={842416127514437269297,
492, 212};
双倍价格[NUM_PRODS]={12.95,14.95,18.95,16.95,21.95,
31.95, 14.95, 14.95, 16.95};
产品列表[产品数量];
对于(int x=0;x
将类和函数从
main
中取出。您发布的代码缺少
}
。就目前而言,<代码>主< /代码>永远不会结束。因为它是C++,所以应该使用<代码> STD::vector < /COD>和<代码>产品< /C> >(单数更好)应该有一个构造函数。在显示代码中有许多基本的问题,涉及范围和声明。函数必须在使用前声明。声明为函数本地的类和结构在函数外部是不可访问的。等等,等等,等等。。。为了编译显示的代码,必须解决所有这些问题。