ifstream and stl

Started by
2 comments, last by nitzan 21 years, 2 months ago
How do I use ifstream and stl together ? To use stl I have to declare "using namespace std", but then fstream.h gets screwed and my ifstream becomes an ambiuous symbol. Any ideas ? Thanks, Nitzan ------------------------- www.geocities.com/nitzanw www.scorchedearth3d.net -------------------------
Advertisement
use <fstream>.
My bad, I figured it out. fstream.h was screwing up because the std:: calls were no longer valid.

So I add std:: to all my stl calls and now it compiles and runs great.

My new question is:

Is the following code (taken from a site explaining STL) valid ?

quote:
vector vec1;
string line;
vec1.clear();
ifstream infile ("stl2in.txt");
while (getline(infile,line,''\n''))
{
vec1.push_back (line);
}


Seems to me it should be :

quote:
std::vector vec1;
std::string line;
vec1.clear();
ifstream infile ("stl2in.txt");
while (getline(infile,line,''\n''))
{
vec1.push_back (line);
}


Nitzan

-------------------------
www.geocities.com/nitzanw
www.scorchedearth3d.net
-------------------------
Looks to me like the difference between those snippets is that the first one makes use of including the entire std namespace (though it isn''t shown.)

Saying:

using namespace std;
vector myVector;

Is valid. The vector class is in the namespace std. Without telling the compiler to use the namespace std by default, to declare a vector you would have to say:

std::vector myVector;

aut viam inveniam aut faciam

MoonStar Projects

This topic is closed to new replies.

Advertisement