【问题标题】:How to debug the error "'fout' was not declared in this scope" [closed]如何调试错误“'fout'未在此范围内声明”[关闭]
【发布时间】:2016-04-24 10:20:00
【问题描述】:

我需要做以下事情:

被认为是 nxn 大小的棋盘,它们愿意成为障碍。要求打印移动疯子所需的最小移动次数,遵守国际象棋规则并避开障碍物,按时从初始位置移动到最终位置。相信傻瓜的初始和最终位置都没有障碍。

这是我的代码:

#include <fstream>
using namespace std;
#define INF 9999

int a[100][100];
int c[100][100];
int ip, jp, is, js;
int i,j;
int m, n;
const int di[] = { -1, 0, 1, 0 },
    dj[] = { 0, 1, 0, -1 };

bool OK( int i, int j );
void Lee();
void Write();
void Read();

int main()
 {
    Read();
    Lee();
    Write();

    return 0;
     }

  void Read()
    {
    ifstream fin("lee.in");
    fin >> n >> m;
    fin >> ip >> jp >> is >> js;
    for ( int i = 0; i < n; i++ )
            for ( int j = 0; j < m; j++ )
            {
                    fin >> a[i][j];
                    c[i][j] = INF;
            }
    fin.close();
      }

      bool OK()
       {
      if ( a[i][j] == 1 ){ return false;} // obstacol, marcat de cifra 1 aici.
    if ( i < 0 || j < 0 || i >= n || j >= m ){}
            return false; // in afara matricei
    return true;  // niciuna din conditiile de mai sus nu au fost adevarate.
             }

    void Lee()
       {
    c[ip][jp] = 0; // punctul de plecare. distanta pana la el fiind de 0.
    bool modif;
    int pas = 0; // numarul de pasi efectuati.
    do
    {
            modif = false;
            for ( int i = 0; i < n; i++ )
                            for ( int j = 0; j < m; j++ )
                                    if ( c[i][j] == pas ) 
                              for ( int d = 0; d < 4; ++d )
                                            {

                                                    int iv = i + di[d];
                                                    int jv = j + dj[d];
                                                    if ( OK( iv, jv) )
                                                    {
                  c[iv][jv] = pas + 1;
                                                            modif = true;
          if ( iv == is && jv == js ) 
                                                                    return;
                                                    }
                                            }
                   pas++;
            } while ( modif );
              }

                void Write()
                  {
    for ( int i = 0; i < n; i++ )
    {
            for ( int j = 0; j < m; j++ )
                            fout << c[i][j] << ' ';
            fout << '\n';
    }
    fout >> c[is][js];


            }

我有 3 个错误“'fout' is not declared in this scope”,我不知道是什么原因。

【问题讨论】:

  • 你在哪里声明了fout?我看不出来。
  • 您的缩进使代码难以阅读。
  • @BrasoveanNicolae “我需要声明 fout 吗?” 当然,就像你已经正确使用 fin 所做的一样,当然使用 std::ofstream
  • 对于严重错误的格式应该有一个关闭选项。任何人都应该如何阅读该代码?
  • 我不明白为什么有人会看到格式如此糟糕的代码并从字面上认为“好吧,太好了,我现在要把它放到互联网上供其他人阅读”。这是怎么回事?!

标签: c++


【解决方案1】:

在代码的适当位置使用ofstream fout(filename); 声明 fout。

【讨论】:

  • 所以我声明了代码末尾的“fout >> c[is][js]'中的'operator>>'不匹配”......
  • 运算符 &gt;&gt; 用于输入流。你有一个ifstream fin("lee.in") 的声明。要执行更多 ifstream() 操作,请声明另一个 fin1 &gt;&gt; c[is][js]。或者它是一个错误,你想做&lt;&lt;
  • 使用 fout >> 你想提取输入吗?如果是,则将 ofstream 更改为 fstream 或使用 ifstream
【解决方案2】:
if ( i < 0 || j < 0 || i >= n || j >= m ){}
     return false; // in afara matricei
return true;  // niciuna din conditiile de mai sus nu au fost adevarate.

if 在条件为真时执行{},因此return false始终被执行。

去掉if末尾的大括号。

【讨论】:

  • @BrasoveanNicolae:停下。您将此地方视为聊天室或客户支持服务台。我们不是在这里一个接一个地修复您的所有错误。在放弃之前花两分钟以上的时间来解决问题并期望我们免费解决问题。花一些时间学习学习,因为这些都是基础知识。在询问之前进行一些研究
  • @BrasoveanNicolae SO 可以帮助解决 一个 特定问题。您的代码有很多,并且清楚地表明您对 C++ 的理解还不够,无法解决手头的任务。你应该推迟编写这个程序,开始阅读一些 C++ 教程/书籍,学习 C++,解决更简单的练习,等你更流利的语言后再回来。
猜你喜欢
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多