编译器声明变量未声明,但它在前一行声明

时间:2014-04-16 00:22:07

标签: c++

我使用MingW作为我的编译器。我已经声明了一个变量strl,但它说它没有在它下面的行声明。

struct idstruct {
    char * path;
    lua_State **state;
};

idstruct ids[] = {};
int nids = 0;

static char* getPath(lua_State **state) {
  std::cout << state;
  for (int on = 0; on < nids; on++)
    idstruct strl = ids[on];
    if (strl.state == state) {
      return strl.path;
    }
  }
}

2 个答案:

答案 0 :(得分:4)

你在for循环体的开头缺少一个大括号。当您达到以下if语句时,strl已超出范围。

答案 1 :(得分:4)

你已经在你的for循环上留下了一个支撑,所以它只是一个单行,尽管你的缩进。因此,变量不在其下面的if语句中。

试试这个:

  for (int on = 0; on < nids; on++) {  // add a brace here
    idstruct strl = ids[on];
    if (strl.state == state) {
      return strl.path;
    }
  }  // and here