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

时间:2018-06-09 16:10:53

标签: c++

我在同一行继续收到此错误两次,但在不同的地方(同一问题):

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 enter\n::";
    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执行此操作并将结果保存为字符串。虽然这样做会遇到错误。

1 个答案:

答案 0 :(得分:2)

这一行缺少大括号:

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

因此,branch only covers the next line(请参阅页面末尾的Notes),这是buffer的声明:

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

根据您的需要,您需要在整个块或其某些部分周围添加花括号,以便buffer保持活着状态。