0

I am trying to write header file. I can write simple headers like add(int x, y) return x+y; . But when I tried to get more complicated, visual studio gave error. I guess error is related to <fstream>. It always shows

"error C2065: 'fstream' : undeclared identifier ".

First line of my cpp file is void get_int(fstream& stream, int offset) (except #include<fstream>) and first definiton of .h file is

#ifndef GET_H
#define GET_H

int get_int(fstream& stream, int offset);

#endif

It goes like this. What is wrong with this code?

Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80

2 Answers2

3

You must do this:

#ifndef GET_H
#define GET_H

#include <fstream>

int get_int(std::fstream& stream, int offset);

#endif

Notice the #include <fstream> and the added std:: prefix. The qualification is needed because all C++ Standard library... things... are defined in that namespace. You should not add a using namespace std; or using std::fstream; in a header, because that pollutes the global namespace and defeats the purpose of the existence of namespace std: people including your header don't expect stuff to be pulled into the global namespace, which may conflict with naming used by others.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • if i use #pragma once, can i use namespace std without any conflict? I am confused because of other answers. – Talha Çolakoğlu Jun 24 '12 at 13:51
  • No. `#pragma once` is just a nonstandard "quick" way of header guards. It is independent of `using` statements. I would even tend to suggest against it (although it is supported by all major compilers), because of its non-standardness. You'll get the hang of and learn to appreciate the `std::` and other namespace prefixes once you notice using the same names in different contexts (but prefixed with different namespace names) simplifies readability in the long run. – rubenvb Jun 24 '12 at 13:56
  • Thank you again, i changed my code again but this was last :) – Talha Çolakoğlu Jun 24 '12 at 14:05
2
#include <fstream>

must be in your header file as well. It goes into the including translation unit, but it still needs to see it because upon expanding inside the .cpp file, it ends up above the #include of fstream. This way you make sure that order won't affect compilation because inclusion guards are in place. It won't try to expand it twice. Also, #pragma once saves kittens.

The reason your ordering gives the same errors is because your header lacks "using std::goeshere" etc.

Code sample as a followup in the comments (preventing namespace pollution):

#ifndef GET_H
#define GET_H

#include <fstream>
using std::fstream;

int get_int(fstream& stream, int offset);

#endif

or (#pragma once should be supported by all decent compilers)

#pragma once

#include <fstream>
using std::fstream;

int get_int(fstream& stream, int offset);
  • 1
    @TalhaÇolakoğlu, don't. Use `std::` before names, because `using namespace std;` pulls way too much symbols into global namespace - which can lead you to really unexpected results. – Griwes Jun 24 '12 at 13:30
  • @TalhaÇolakoğlu Or you can use "using std::fstream" for identifiers of choice which are located in a specific namespace, in this case namespace std –  Jun 24 '12 at 13:35
  • Thanks, this is more useful than other :) But i think this is harmful.I understand that other answer. – Talha Çolakoğlu Jun 24 '12 at 13:39
  • `#pragma once` works most of the time, with many compilers. Is that good enough? See [Why isn't pragma once an ISO standard?](http://stackoverflow.com/questions/1695807/why-isnt-c-cs-pragma-once-an-iso-standard) – Bo Persson Jun 24 '12 at 13:44