tags:

views:

164

answers:

5

Hey guys, I'm writing the simplest thing ever, just creating an ifstream to read in a text file and I have a weird error. Here is the code (note : the '<' missing for iostream and fstream are well written in my code but I couldn't write them here)

#include "genlib.h"
#include "simpio.h"
#include "random.h"
#include "vector.h"
#include "map.h"
#include <iostream>
#include <fstream>

int main() {
 ifstream in;
 in.open("Hamlet.txt");
 if (in.fail()) Error("Could not open file");
 return 0;
}

I get the following error after the ifstream in; line : "error : expected unqualified-id before '=' token"

Any idea what's going wrong ?

Thanks

A: 

Try opening the file directly in the constructor:

ifstream inf ( "Hamlet.txt" , ifstream::in );
Justin Ethier
A: 

use std::ifstream

(and provide compilable code; those are not all standard headers, and what is Error()?)

stijn
+3  A: 

The only thing unusual past ifstream in; is the Error call. My wild guess is that it's a poorly written macro. Try this instead:

int main() {
 ifstream in;
 in.open("Hamlet.txt");
 if (in.fail()) { Error("Could not open file"); }
 return 0;
}

Note the new braces around Error.

Gunslinger47
good point about the macro.
stijn
+1  A: 

My guess is that in one of your own include files ("genlib.h" and "simpio.h" seem non-Standard), that you're #defined "in"

James Curran
+2  A: 

You need to add

using namespace std;

to use arbitrary names from the library without qualification. Otherwise, the declaration must be

std::ifstream in;

There is also the option

using std::ifstream;

but I wouldn't recommend it, since you probably won't be writing out std::ifstream all that often.

Potatoswatter