Solved

std namespaace and fstream.h

Posted on 2001-06-20
7
555 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 200 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
Technology Partners: 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Grammars for C C++ and java 1 138
Create a path if not exists 7 108
One named event, multiple event handlers 2 43
How to add columns to a RAD Studio FMX grid at design time 1 30
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

756 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