c++ - 编译器说变量没有声明,但它在前一行声明了

标签 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;
    }
  }
}

最佳答案

您在 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

关于c++ - 编译器说变量没有声明,但它在前一行声明了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23096826/

相关文章:

c++ - 在 OpenCV 中将 RGB 转换为黑白

c++ - 在 for 循环和变量值中,此代码出现多个错误;

c++ - 在 `std::get<I>` 上使用 `std::tuple` 是否保证对于 `I` 的不同值是线程安全的?

c++ - 无法强制 clang CompilerInstance 对象将 header 解析为 C++ 文件

c++ - 检查该宏参数是否是允许的参数之一?

c++ - 如何使用 VARIANT 指定颜色

c++ - 带有 ftime 函数的 Visual Studio 2015/2017 年 2038 错误

c++ - 处理创建进程的 AppCrash

c++ - gdb - 列出当前函数的源而不输入其名称

c++ - 固定大小的容器,其中元素已排序并可以提供指向 C++ 中数据的原始指针