魔方函数 C++

发布于 2024-10-16 23:39:07 字数 509 浏览 2 评论 0原文

这是我的魔方的最后一个函数,由于某种原因,它给了我一个错误,数组下标有“'[int]'”,但我不知道这意味着什么,如果有人可以帮助解释我必须做什么做。

bool Square::is_magic()
{
    for (i = 0; i < size-1; i++)
    {
        if (sum_row[i] != sum_row[i+1])
            return false;

    if (sum_col[i] != sum_col[i+1])
        return false;
        }

    if (sum_row[0] != sum_col[0])
        return false;

    if (sum_row[0] != sum_maindiag[0])
        return false;

    if (sum_row[0] != sum_other[0])
        return false;

    return true;
}

This is my last function for my magic square and for some reason it's giving me an error that there is "'[int]' for array subscript" but I don't know what that means, if someone could help explain what I have to do.

bool Square::is_magic()
{
    for (i = 0; i < size-1; i++)
    {
        if (sum_row[i] != sum_row[i+1])
            return false;

    if (sum_col[i] != sum_col[i+1])
        return false;
        }

    if (sum_row[0] != sum_col[0])
        return false;

    if (sum_row[0] != sum_maindiag[0])
        return false;

    if (sum_row[0] != sum_other[0])
        return false;

    return true;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幼儿园老大 2024-10-23 23:39:07

好吧,每个人都曾经是初学者。我强烈推荐你阅读一两本关于 C++ 的书。 (我个人是通过“21天学习c++”学习编程的,很多人抱怨,但这对我来说是一个好的开始)。

以及代码。不确定这是否是您需要的,它应该是这样的:

bool Square::is_magic()
{
   int i;
   for (i = 0; i < size-1; i++)
   {
       if (sum_row[i] != sum_row[i+1])
          return false;

       if (sum_col[i] != sum_col[i+1])
          return false;
   }

   if (sum_row[0] != sum_col[0])
         return false;

   if (sum_row[0] != sum_maindiag[0])
         return false;

   if (sum_row[0] != sum_other[0])
         return false;

   return true;

}

一些评论:

  1. 在 if、for、while 语句之后不需要括号为 1 个命令

    在 if、for、while 语句

  2. 建议使用 if ->否则如果 ->别的。在这里,这并不重要,因为一旦发现不正确的地方,您就会跳出函数,但如果您继续代码,即使没有必要,您也会检查其他语句。

  3. 习惯某种风格,创造自己的风格或复制别人的风格。我个人是这样使用括号的:

    如果(某事!=某事Else){
    doSomeNastyThings();
    doEvenMore();
    }

好运..

编辑:添加变量声明 int for 语句,更新括号(聪明的想法,因为最后 3 个 if-s 不使用索引)

Ok everybody was beginer at some time. I really recommend you to read one or two books focused on c++. (Personally I learned programming with "Learn c++ in 21 days", many complain but it was good start for me).

And for the code. Not sure that it's what you need, it should go like this:

bool Square::is_magic()
{
   int i;
   for (i = 0; i < size-1; i++)
   {
       if (sum_row[i] != sum_row[i+1])
          return false;

       if (sum_col[i] != sum_col[i+1])
          return false;
   }

   if (sum_row[0] != sum_col[0])
         return false;

   if (sum_row[0] != sum_maindiag[0])
         return false;

   if (sum_row[0] != sum_other[0])
         return false;

   return true;

}

Some comments:

  1. You don't need brackets for 1 command after if,for,while statement

  2. Suggest using if -> else if -> else. Here it doesn't matter because you jump out of function as soon as you find something not correct, but in case you would continue in code you would check other statements even if it wasn't necessary.

  3. Get used to some style, make your own or copy someone's. Personally I use brackets this way:

    if (something != somethingElse){
    doSomeNastyThings();
    doEvenMore();
    }

Good luck..

Edit: added variable declaration int for statement, updated brackets (clever idea as last 3 if-s aren't using index)

破晓 2024-10-23 23:39:07

如果语句的格式如下:

if (condition) {
  do_this()
}

不是这样:

{
if (condition)
  do_this()
}

它们是您格式化代码的方式,您在两行后关闭 for 循环,我想这不是您想要做的(因为您指的是 var我后来)。

If statements are formatted like this:

if (condition) {
  do_this()
}

not like this:

{
if (condition)
  do_this()
}

They way you're formatting your code you're closing your for loop after two lines, which I imagine is not what you're trying to do (since you're referring to var i afterwards).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文