I'm trying to compile this simple C++ program in Code Blocks:
#include <string>
#include <fstream>
#include <streambuf>
#include <sstream>
std::ifstream t("C:/Windows/System32/drivers/etc/hosts-backup.txt");
std::stringstream buffer;
buffer << t.rdbuf();
And I get this error:
||=== Build: Debug in hostapp2 (compiler: GNU GCC Compiler) ===|
C:\Users\Flights Trainer\Desktop\hostapp2\main.cpp|7|error: 'buffer' does not name a type|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
I've been googling "does not name a type" all night and everything I've found points to using a class before it has been declared but I dont understand where I am doing that.
What am I doing wrong?
You can't put arbitrary statements at the file scope in C++, you need to put them in a function.
#include <string>
#include <fstream>
#include <streambuf>
#include <sstream>
int main () {
//These are now local variables
std::ifstream t("C:/Windows/System32/drivers/etc/hosts-backup.txt");
std::stringstream buffer;
//We can write expression statements because we're in a function
buffer << t.rdbuf();
}