Solved

std namespaace and fstream.h

Posted on 2001-06-20
7
554 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
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C Language combined operators 28 114
Least Squares Curve Fitting 4 96
trigs fail! I thought I knew how to do trignometry 3 75
Beginner to Unreal Engine 4 5 94
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

856 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