0

我从来没有经历过这样的事情。我正在使用 cout 语句来帮助我调试一个小程序,一旦我对我的代码感到满意,我就注释掉了 cout。现在,代码不再有效。下面是注释掉 cout 的代码。

该程序的目的是测试两个硬编码的布尔二维数组在每行上是否有奇数个真实语句。因此,第一个数组应该返回 true,第二个数组应该返回 false。将 cout 语句注释掉后,两者都返回 false。

#include <iostream>

using namespace std;

template <size_t size_y>
bool findEvenDegrees(bool mapArray[][size_y])
{
    bool returnValue;
    for (int x=0; x<size_y; x++)
    {
        int result = 0;
        for (int y=0; y<size_y; y++)
        {
            result = result + mapArray[x][y];
            //the line below causes the problem
            cout << mapArray[x][y] << "\t" << result << "\t" << x << endl;
        }
        if (result%2 == 1)
        {
            returnValue = false;
            break;
        }
    }
    if (returnValue== false)
    {
        return returnValue;
    }
    else
    {
        return true;
    }
}

int main()
{
    bool array1[][6] =
    {
        {false,true,true,false,false,false},
        {true,false,false,true,false,false},
        {true,false,false,true,false,false},
        {false,true,true,false,true,true},
        {false,false,false,true,false,true},
        {false,false,false,true,true,false}
    };
    bool array2[][8] =
    {
        {false,true,true,false,false,false,false,false},
        {true,false,false,true,false,false,false,false},
        {true,false,false,true,false,false,false,false},
        {false,true,true,false,true,false,false,false},
        {false,false,false,true,false,true,true,false},
        {false,false,false,false,false,true,false,true},
        {false,false,false,false,true,false,false,true},
        {false,false,false,false,false,true,true,false}
    };
    bool answer1 = findEvenDegrees(array1);
    bool answer2 = findEvenDegrees(array2);
    if (answer1 == true)
    {
        cout << "Array 1 has a even degree for every switch." << endl;
    }
    else
    {
        cout << "Array 1 has a odd degree for at least one switch." << endl;
    }
    if (answer2 == true)
    {
        cout << "Array 2 has a even degree for every switch.";
    }
    else
    {
        cout << "Array 2 has a odd degree for at least one switch.";
    }
    return 0;
}
4

2 回答 2

2

你永远不会初始化returnValue. 如果它恰好开始,false它将保持这种状态并且函数将返回false

于 2013-06-20T03:10:19.013 回答
1

首先,我稍微清理了您的代码,然后得出:

#include <iostream>

template <size_t S>
bool findEvenDegrees(bool (&themap)[S][S]) {
  for( bool(&row)[S]: themap ) {
    bool is_degree_odd = false;
    for( auto col: row )
      is_degree_odd ^= col;
    if( is_degree_odd )
      return false;
  }
  return true;
}

int main()
{
  using std::cout;
  using std::endl;

  bool array1[6][6] = {
    {false,true,true,false,false,false},
    {true,false,false,true,false,false},
    {true,false,false,true,false,false},
    {false,true,true,false,true,true},
    {false,false,false,true,false,true},
    {false,false,false,true,true,false}
  };
  cout << "Array 1 has an "
    << (findEvenDegrees(array1) ? "even degree for every" : "odd degree for at least one")
    << " switch." << endl;

  bool array2[8][8]= {
    {false,true,true,false,false,false,false,false},
    {true,false,false,true,false,false,false,false},
    {true,false,false,true,false,false,false,false},
    {false,true,true,false,true,false,false,false},
    {false,false,false,true,false,true,true,false},
    {false,false,false,false,false,true,false,true},
    {false,false,false,false,true,false,false,true},
    {false,false,false,false,false,true,true,false}
  };
  cout << "Array 2 has an "
    << (findEvenDegrees(array2) ? "even degree for every" : "odd degree for at least one")
    << " switch." << endl;

  return 0;
}

在清理它的过程中,我if(result%2 == 1) { resultValue = true; break; }通过在找到第一个奇数行时有效地返回来消除 , 。当我消除resultValue变量时,我还杀死了@sth 提到的“未初始化”错误。

于 2013-06-20T03:46:17.883 回答