I have two files, my_program.cpp and its header my_program.h.
my_program.cpp contains only this:
#include "my_program.h"
using namespace std;
my_program.h contains a pointer to a function which returns a wstring, as follows:
using namespace std;
typedef wstring (*my_function)(wstring, int, int, int, int);
The program doesn’t compile in this state (stops at typedef… saying that ISO C++ forbids declaration of ‘wstring’ with no type) but if I add #include <iostream> before #include "my_program.h" in the .cpp file, the program miraculously compiles.
Why does this happen? I just hope I didn’t make a foolish mistake and I’m now going to be laughed at.
You really should include
<string>.From the sound of things, your compiler’s
<iostream>happens to include<string>, so it works, but on a different compiler it may not. C++ allows a standard header to include other standard headers, but doesn’t require it. In some cases, you get only a declaration of the class, so some things work, and others don’t.At least in my experience, this is also an issue that’s likely to change from one version of a compiler to the next, so even if you don’t intend to port to anything else, your code may quit working just due to a seemingly trivial upgrade unless you include the right header.