Anon
Anon

Reputation: 1354

ifstream of <fstream> won't compile

I #include these headers:

#include <iostream>
#include <fstream>

but however this piece of code:

ifstream inFile;

still wont compile. what could be the problem? Im using Visual Studio 2010, Win32 C++.

Upvotes: 0

Views: 1456

Answers (2)

paxdiablo
paxdiablo

Reputation: 881403

You can put a using namespace std; at the top of your code so you don't have to fully qualify standard C++ stuff, but it's considered bad form by a large number of developers.

I simply prefix the standard stuff with std::, which makes the code longer:

std::cout << "Hello, world.\n";

but keeps me out of trouble vis-a-vis namespace clashes.

The following transcript shows the use of std:: prefixes in action:

$ cat testprog.cpp
    #include <iostream>
    #include <fstream>

    int main (void) {
        int n;
        std::ifstream inFile("input.txt");
        inFile >> n;
        std::cout << "File contained " << n << '\n';
        return 0;
    }

$ cat input.txt
42

$ g++ -Wall -Wextra -o testprog testprog.cpp ; ./testprog
File contained 42

Upvotes: 4

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

The type is std::ifstream. You must write it out in full, unless you brought the qualified name into scope by another means.

Upvotes: 3

Related Questions