No .h fot C++ Standard Library head file and g++ doesn't import the std namespace by default

by Forrest Sheng Bao http://fsbao.net

I want to talk about two common errors people encounter when they compile old programs, especially when they are using recent version of g++. They may wonder why this code can be compiled in g++3.4 but not g++4.3.

A lot people get shock when they see this:

error: fstream.h: No such file or directory

The answer to solve this problem is that "Header files in the C++ Standard Library do not use the .h extension; have have no extension," -Kyle Loudon, C++ Pocket Reference, p.5, O'Reilly, 2003. So instead of saying

#include 

, you should say

#include 

The second common error is that you have included the library which defines a function, but you still got this:

error: ‘ifstream’ was not declared in this scope

Well, recent versions of g++ does not import std namespace by default. So, you have to add this line

using namespace std;

Above two issues had been address in textbooks a long time ago, at least when I was a college freshman. But I really do not understand why people are still thinking in the old way after so many years.

No comments: