魔方函数 C++
这是我的魔方的最后一个函数,由于某种原因,它给了我一个错误,数组下标有“'[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,每个人都曾经是初学者。我强烈推荐你阅读一两本关于 C++ 的书。 (我个人是通过“21天学习c++”学习编程的,很多人抱怨,但这对我来说是一个好的开始)。
以及代码。不确定这是否是您需要的,它应该是这样的:
一些评论:
在 if、for、while 语句之后不需要括号为 1 个命令
在 if、for、while 语句
建议使用 if ->否则如果 ->别的。在这里,这并不重要,因为一旦发现不正确的地方,您就会跳出函数,但如果您继续代码,即使没有必要,您也会检查其他语句。
习惯某种风格,创造自己的风格或复制别人的风格。我个人是这样使用括号的:
如果(某事!=某事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:
Some comments:
You don't need brackets for 1 command after if,for,while statement
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.
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)
如果语句的格式如下:
不是这样:
它们是您格式化代码的方式,您在两行后关闭 for 循环,我想这不是您想要做的(因为您指的是 var我后来)。
If statements are formatted like this:
not like 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).