无法在循环中给敲击 array/variable 赋值

cannot give value to struck array/variable in a loop

我正在尝试为多边形提供坐标。 arrayxarrayy 是包含我的 x 和 y 坐标的数组。问题是,我无法将数组的值赋予 polygon Point 结构。

#include <iostream>
#include <fstream>
#include <string>

struct Point
{
    int x,y;
};

int main()
{    

  Point polygon[]={};
  int n=0, arrayx[1024] = {0} , arrayy[1024] = {0};

   //input
   ifstream iFile;
   iFile.open("in.txt");

        while(iFile >> arrayx[n] >> arrayy[n])
        {

            n = n + 1;
        }

    iFile.close(); 

    int k=n;          
    for (int j=0; j<= k; j++)
    {
        polygon[j] = (Point){arrayx[j],arrayy[j]}; //something wrong here

        //output
        ofstream myfile;
        myfile.open ("out3.txt", ios::app);
        myfile  <<"k = " <<k << "----" <<j << ") polygon.x = "<< polygon[j].x  <<" arrayx = "<<arrayx[j] << " polygon[j].y = "<< polygon[j].y  <<" arrayy = "<<arrayy[j] << "\n";
        myfile.close();
    }
}

我也试过像下面这样更改代码:

polygon[j].x = arrayx[j];
polygon[j].y = arrayy[j];

输入:

260 673
565 735
780 619
746 408
333 400

输出:

k = 260 
j = 673    
polygon[j].x = 7209071     
arrayx = 0 
polygon[j].y = 7471220 
arrayy = 0

突然 k 将其值更改为 260。循环 j 的变量变为 673,(不是 j=0,1,2,3,4,5 只有一次循环和中断) 即使 arrays[673] 是空的,polygon 也会得到一些数字。

我对c++的经验不多,我一个人很难解决。

您将无法为数组分配任何内容,因为数组为空

Point polygon[]={};

这应该会导致编译器错误,因为标准 C++ 中不允许大小为 0 的数组。