?
Solved

std namespaace and fstream.h

Posted on 2001-06-20
7
Medium Priority
?
560 Views
Last Modified: 2008-02-07
When using :
"using namespace std" and fstream.h I get the following error:
"ifstream:ambiguous symbol"

Anyone have a clue Why?
0
Comment
Question by:shayb022800
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 4

Expert Comment

by:IainHere
ID: 6209744
Use
#include <fstream>
instead, because it's the standard C++ (different from C header file).  This should fix the problem.
0
 
LVL 4

Accepted Solution

by:
IainHere earned 800 total points
ID: 6209787
BTW: fstream.h is included only for backward compatability, all new C++ programs should only use fstream.  Table 11 in chapter 17 of the C++ standard draft (dec 1996, most recent I can find for download) lists the 32 standard C++ headers.

The std namespace is used in the C++ headers, not for the C ones.  Hence the problem you saw.
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6209807
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 6209948
This is caused because header files with the ".h" extension have the functions and classes in the global namespace, and extension-less header files use the std namespace.
If you mix extension-less header files with *.h header files, and use "using namespace std" the compiler will see two sets of the same declarations, and will not know which one to use. (ambiguity)

For this to happen it doesn't require you to mix both <fstream> and <fstream.h>.  You could be mixing <fstream.h> and <iostream>, and you will still get the "ambiguity" error.
0
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6211537
Assume  you put   "using namespace std" in code and the code has a identifier "ifstream".

The compiler tries to understand what "ifstream" means  in your code.
whether It is std::ifstream or ::ifstream ?

It is easy for compiler if your code and  the header files in your code have
only std::ifstream or have only ::ifstream.

However,the compiler can not decide what "ifstream" represents for when your code
and your header files have both std::ifstream and ::ifstream. In this case,  "ifstrea"
is an ambiguous symbol"


For solving this problem, you need to help the compiler understanding what you mean
when you use "ifstream". As previous comments, don't use fstream.h is
the easy way for fixing this bug is. However, if you still want to use fstream.h, you need
to put #include files and "using namespace std"  in correct order and
you need to specify to use std::ifstream or ::ifstream

The following is an example :

#include <fstream.h>
#include <fstream>
using namespace std;

void foo()
{
    ::ifstream  old_stream;
    std::ifstream  new_stream;

    // "ifstream:ambiguous symbol" if using the next line
    // ifstream ambiguous_stream;
}

The above code doesn't error when compiling.

Note : the following code will has error at compiler time because
 using namespace in incorrect order

#include <fstream>
using namespace std;
#include <fstream.h>

.....

0
 
LVL 11

Expert Comment

by:griessh
ID: 6819937
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "IanHere" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner

0
 

Expert Comment

by:ComTech
ID: 6837653
Accepted as per recommendation.

ComTech
CS Admin @ EE
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question