c++ "not declared in this scope" 错误

c++ "not declared in this scope" error

我不断收到一条错误消息,告诉我我没有在 if(!buffer.empty) 循环下的范围内定义缓冲区。

有人对我应该做什么和做错了什么有什么建议吗?

#include <fstream>  // this is to import the ifstream and ofstream objects
#include <iostream>  // this is to import the cin and cout objects
#include <stack> 
using namespace std;

// precondition: theFile refers to a stream that has been opened already
// postcondition: the contents of the file have been read and output to the console

void read_file( ifstream& theFile ) {
    stack buffer;  // this string will read in the buffer from the file 

    //while there are still things in the file to read in, read it in and output to console.
    while( theFile.eof() == false ) {
        buffer.push(theFile);
        //cout << buffer << endl; // print the string and a newline 
    }

    if( !buffer.empty() ) {
        cout << buffer.top() << endl;
        buffer.pop();
    }else{
        cout << "uh oh!" << endl;
    }
}

int main() {
    ifstream theInputFile;
    theInputFile.open("input.txt"); // Open the file with the name "inputFile.txt". 

    // pass the file stream into the read_file() function. 
    read_file( theInputFile );
    theInputFile.close();
}

所以缓冲区是一个堆栈。堆什么? stack<int> buffer 可以,或者 stack<char> buffer,或者你需要一堆的任何东西。

我不知道你需要一堆什么。我注意到您在推动 theFile,这没有意义。这可能是有道理的:

while( theFile.eof() == false ) 
{
    theFile >> something;
    if (! theFile) break; //if we reached eof or had other problems, just quit
    buffer.push(something);
}

取决于你想用空格做什么,如果 somethingchar, char*,或者 string