错误:使用未声明的标识符"缓冲区"C++

error: use of undeclared identifier 'buffer' C++

本文关键字:标识符 缓冲区 C++ 未声明 错误      更新时间:2023-10-16

我在同一行上两次收到此错误,但在不同的位置(同一问题(:

Documents/JoeInstaller.cpp:33:43: error: use of undeclared identifier 'buffer'
memset(buffer, (char)NULL, sizeof(buffer)))
^
Documents/JoeInstaller.cpp:33:16: error: use of undeclared identifier 'buffer'
memset(buffer, (char)NULL, sizeof(buffer)))

这是我到目前为止的代码:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <stdio.h>
#include <curl/curl.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
float pythonver;
int main(int argc, char* argv[]){
cout << "Press entern::";
if (cin.get() == 'n')
char buffer[1000];
FILE* progOutput;
progOutput = popen("which python3.6", "r");
if (!progOutput) {
cerr<<"npopen failedn";
exit(1);
}
memset(buffer, (char)NULL, sizeof(buffer)))
if (fread(buffer, sizeof(char), sizeof(char) * sizeof(buffer), progOutput) < 0) {
cerr<<"nfread failedn";
exit(1);
}
if (pclose(progOutput) < 0) {
cerr<<"npclose failedn";
exit(1);
}
pythonver << atof(buffer<<endl.c_str());
if (pythonver) {
cout << "Hooray! Now you need to install python3.6 or later from source!";
}

总之,我想做的是获取python的版本。我正在通过 popen 执行此操作并将结果保存到字符串中。执行此操作时会遇到错误。

此行缺少一个大括号:

if (cin.get() == 'n')

因此,分支仅涵盖下一行(请参阅页面末尾的Notes(,即buffer声明:

if (cin.get() == 'n')
char buffer[1000];
// buffer is out of scope here

根据您想要的内容,您需要在整个块或其某些部分周围添加大括号,以保持buffer在使用时保持活动状态。