Solved

std namespaace and fstream.h

Posted on 2001-06-20
7
538 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Accepted as per recommendation.

ComTech
CS Admin @ EE
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now