【问题标题】:Cannot apply Indexing with [] to an expression type application.MatrixVariables无法将带有 [] 的索引应用于表达式类型 application.MatrixVariables
【发布时间】:2016-03-03 14:45:00
【问题描述】:

刚接触编程并试图了解数组和结构。我似乎遇到了数组问题,并且不确定我这样做是否正确。

namespace ConsoleApplication1
{    
   public struct MatrixVariables
   {
       public int x, y;
       public MatrixVariables (int lowerV, int upperV)
       {
           x = lowerV;
           y = upperV;
       }
   }

   class Program
   {
       static void Main(string[] args)
       {
           int[,] a = new int[,] { { 0, 1, 1, 0 },{0,0,1,0}, {1,0,1,1}, {1,1,1,1}, {1,0,0,1} };
           int[,] t = new int[,] { { 30, 10, 50, 25, 14 } };
           MatrixVariables upperV = new MatrixVariables();
           MatrixVariables lowerV = new MatrixVariables();

           int maxP = 6;
           int maxV = 6;
           int p = 0;
           int v = 0;
           for (p=0;p<maxP;p++)
           {
               for (v=0; v == maxV;v++)
               {
                   if (a[p,v] ==1)
                   {
                       if (upperV[v] > t[p])
                       {
                           upperV[v] = t[p];
                       }
                   }
               }
           }   
       }
   }
}

【问题讨论】:

  • 期望索引器做什么?
  • 有什么问题?你还没有列出一个
  • @BugFinder:错误信息在标题中,虽然帖子肯定会更清楚。

标签: c# arrays struct


【解决方案1】:

你声明MatrixVariables upperV = new MatrixVariables();

这与int i = new int(); 并没有真正的不同,你要求你的结构的一个大小块。因此它不能有子部分。

您的结构是UpperV.xUpperV.y,而不是任何东西的数组。 x 和 y 都将为零,因为您还没有调用您的创建者。但是你也没有使用 x 或 y 来做任何事情,即使它是一个数组。

【讨论】:

  • if (upperV[v] > t[p]) { upperV[v] = t[p]; } 这里就是这条线。在标题中给出错误信息。我真的不明白你的回答。你有代码中的例子吗?
  • 完全正确,但它不是一个数组 - 你没有要求一个............所以你不能这样做......这也不是在那个结构中设置 2 个变量,x或者 y.. 你还说过,当 a 中的数组长 4 个而不是 5 时,你正在循环 0->4 ......那里有很多错误。
  • 那么lowerV和lowerV需要是数组而不是结构体吗?
  • 就目前而言,您的代码将在多个级别上失败。你还没有 lowerV 的代码 .. 你到底想做什么?
猜你喜欢
  • 1970-01-01
  • 2019-05-07
  • 2011-04-06
  • 2019-03-20
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-08
相关资源
最近更新 更多