Solved

std namespaace and fstream.h

Posted on 2001-06-20
7
557 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
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 149
IdTCPClient1->Disconnect(); not working 3 92
SNMP error No Such Object available on this agent at this OID 3 446
Unresolved External Symbols 3 157
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

739 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