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

c++ "not declared in this scope" error

本文关键字:scope 错误 this declared not C++ in      更新时间:2023-10-16

我一直收到一个错误,告诉我没有在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,则取决于要对空白进行什么处理。