递归函数中的类变量访问
class variable access within recursive function
对于一个介绍程序,我们被要求构建一个程序,该程序可以找到给定大小的每一个可能的工作幻方。我在从递归函数中修改类变量时遇到问题。每当我尝试的数字组合产生一个幻方时,我就试图增加找到的幻方的数量。
更具体地说,我正在尝试在函数recursiveMagic()中修改numSquares。在特定行设置断点后,变量numSquares不会改变,即使我正在递增它。我认为这与递归有关,但我不确定。如果你想提供一些建议,我很感激。
//============================================================================
// Name : magicSquare.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
/**
* MagicSquare
*/
class MagicSquare {
private:
int magicSquare[9];
int usedNumbers[9];
int numSquares;
int N;
int magicInt;
public:
MagicSquare() {
numSquares = 0;
for (int i = 0; i < 9; i++)
usedNumbers[i] = 0;
N = 3; //default is 3
magicInt = N * (N * N + 1) / 2;
}
MagicSquare(int n) {
numSquares = 0;
for (int i = 0; i < 9; i++)
usedNumbers[i] = 0;
N = n;
magicInt = N * (N * N + 1) / 2;
}
void recursiveMagic(int n) {
for (int i = 1; i <= N * N + 1; i++) {
if (usedNumbers[i - 1] == 0) {
usedNumbers[i - 1] = 1;
magicSquare[n] = i;
if (n < N * N)
recursiveMagic(n + 1);
else {
if (isMagicSquare()) {
numSquares++; //this is the line that is not working correctly
printSquare();
}
}
usedNumbers[i - 1] = 0;
}
}
}
//To efficiently check all rows and collumns, we must convert the one dimensional array into a 2d array
//since the sudo 2d array looks like this:
// 0 1 2
// 3 4 5
// 6 7 8
//the following for-if loops convert the i to the appropriate location.
bool isMagicSquare() {
for (int i = 0; i < 3; i++) {
if ((magicSquare[i * 3] + magicSquare[i * 3 + 1] + magicSquare[i * 3 + 2]) != magicInt) //check horizontal
return false;
else if ((magicSquare[i] + magicSquare[i + 3] + magicSquare[i + 6]) != magicInt) // check vertical
return false;
}
if ((magicSquare[0] + magicSquare[4] + magicSquare[8]) != magicInt)
return false;
if ((magicSquare[6] + magicSquare[4] + magicSquare[2]) != magicInt)
return false;
return true;
}
/**
* printSquare: prints the current magic square combination
*/
void printSquare() {
for (int i = 0; i < 3; i++)
cout << magicSquare[i * 3] << " " << magicSquare[i * 3 + 1]
<< " " << magicSquare[i * 3 + 2] << endl;
cout << "------------------" << endl;
}
/**
* checkRow: checks to see if the current row will complete the magic square
* @param i - used to determine what row is being analyzed
* @return true if it is a working row, and false if it is not
*/
bool checkRow(int i) {
i = (i + 1) % 3 - 1;
return (magicSquare[i * 3] + magicSquare[i * 3 + 1] + magicSquare[i * 3 + 2]) == magicInt;
}
int getnumSquares() {
return numSquares;
}
}; //------End of MagicSquare Class-----
int main() {
MagicSquare square;
cout << "Begin Magic Square recursion:" << endl << "------------------"
<< endl;
square.recursiveMagic(0);
cout << "Done with routine, returned combinations: " << square.getnumSquares() << endl;
return 0;
}
数组被覆盖,导致覆盖numSquares字段。
class MagicSquare {
private:
int magicSquare[9];
int usedNumbers[9];
的更改
class MagicSquare {
private:
int magicSquare[10];
int usedNumbers[10];
同样在初始化器中,循环说<9但是你想说的是<10.或者只是使用memset更好。
相关文章:
- 访问类成员而不复制此变量或互斥变量
- 分段 访问私有类变量时出错
- 如何访问基类向量中的子类变量?(对于实体组件系统)
- 访问类对象以与全局变量进行比较
- 如何在构造函数中访问类变量以分配它们,而无需在C++中使用此指针
- 访问类成员的成员变量时 cpp 程序挂起
- 类变量无法从类方法访问
- 从静态成员函数访问私有非静态类变量 - C++
- 如何使用对象数组访问类中的变量?
- 无法使用子类变量访问超类公共成员
- 为什么我无法使用继承类访问私有变量?
- 为什么可以从类中的对象访问类的私有变量
- 创建函数来访问类变量是一种好的做法吗
- 从std::thread访问类变量
- 如何访问类变量?公共方法/getters与继承.优点和缺点是什么
- 在使用头文件/源文件时访问类变量的麻烦
- 如何从外部"C"函数访问类变量?
- 无法访问类变量
- 访问类变量时出现问题
- 友元函数无法访问类变量