473,586 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file >> out.rdbuf(); fails to compile, why?

My compiler (Borland C++) fails to compile this code:

ifstream file(filename.c _str());
ostringstream out;
file >> out.rdbuf();

with the error in the 3rd line above that:
[C++ Error] realmain.cpp(83 ): E2015 Ambiguity between
'_STL::basic_is tream<char,_STL ::char_traits<c har> >::operator >>(bool &)'
and '_STL::basic_is tream<char,_STL ::char_traits<c har> >::operator >>(void *
&)'
But it compiles the following without error.

ifstream file(filename.c _str());
ostringstream out;
streambuf * outbuf = out.rdbuf();
file >> outbuf;
Why the discrepancy?
Jul 22 '05 #1
4 2182
Siemel Naran wrote:
My compiler (Borland C++) fails to compile this code:

ifstream file(filename.c _str());
ostringstream out;
file >> out.rdbuf();

with the error in the 3rd line above that:
[C++ Error] realmain.cpp(83 ): E2015 Ambiguity between
'_STL::basic_is tream<char,_STL ::char_traits<c har> >::operator >>(bool &)'
and '_STL::basic_is tream<char,_STL ::char_traits<c har> >::operator >>(void *
&)'
The 'std::istream:: operator >> (std::streambuf *)' overload is a better
match than either of those. I suspect it's missing.
But it compiles the following without error.

ifstream file(filename.c _str());
ostringstream out;
streambuf * outbuf = out.rdbuf();
file >> outbuf;
Did you run it? I imagine that ">>" is calling 'std::istream:: operator
(void * &)', and trying to read a raw pointer.


I tried to test this hypothesis using Borland C++ Builder 6.0 and found
that it can't read raw pointers either. So you're on to a loser. Here's
the test.

#include <istream>
#include <ostream>
#include <sstream>
#include <iostream>
#include <limits>

int main ()
{
void * p = reinterpret_cas t <void *> (0xdeadbeef);
void * q;

std::ostringstr eam out;
out << p;

std::istringstr eam in (out.str ());

if (in >> q) std::cout << std::hex << q << '\n';
else std::cout << "fail\n";

// pause to view results if running from the Borland IDE
std::cin.ignore (std::numeric_l imits <std::streamsiz e>::max (), '\n');
}

Outputs "0xdeadbeef " on g++ 3.3.3, "fail" on bcb 6.0.
Why the discrepancy?


Your code is pretty much correct. Complain to Borland, and/or upgrade to
the latest version.

--
Regards,
Buster
Jul 22 '05 #2
On Fri, 17 Dec 2004 10:29:18 GMT in comp.lang.c++, "Siemel Naran"
<Si*********@RE MOVE.att.net> wrote,
My compiler (Borland C++) fails to compile this code:

ifstream file(filename.c _str());
ostringstream out;
file >> out.rdbuf();


You wanted the incantation
out << file.rdbuf();

http://groups.google.com/gr*********...ing.google.com
http://groups.google.c om/gr************* ************@ID-79865.news.uni-berlin.de

Jul 22 '05 #3
Buster wrote:
int main ()
{
void * p = reinterpret_cas t <void *> (0xdeadbeef);
void * q;

std::ostringstr eam out;
out << p;
Undefined behaviour -- using the value of an invalid pointer

std::istringstr eam in (out.str ());

if (in >> q) std::cout << std::hex << q << '\n';
else std::cout << "fail\n";

// pause to view results if running from the Borland IDE
std::cin.ignore (std::numeric_l imits <std::streamsiz e>::max (), '\n'); }

Outputs "0xdeadbeef " on g++ 3.3.3, "fail" on bcb 6.0.

Since you have invoked UB, "fail" is a conforming output.

Jul 22 '05 #4
Old Wolf wrote:
Buster wrote:
int main ()
{
void * p = reinterpret_cas t <void *> (0xdeadbeef);
void * q;

std::ostringstr eam out;
out << p;

Undefined behaviour -- using the value of an invalid pointer


Oh, don't be silly. Change it to:
"int x; void * p = reinterpret_cas t <void *> (& x);"
and it still won't work.
std::istringstr eam in (out.str ());

if (in >> q) std::cout << std::hex << q << '\n';
else std::cout << "fail\n";

// pause to view results if running from the Borland IDE
std::cin.ignore (std::numeric_l imits <std::streamsiz e>::max (),


'\n');
}

Outputs "0xdeadbeef " on g++ 3.3.3, "fail" on bcb 6.0.


Since you have invoked UB, "fail" is a conforming output.

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3078
by: Michael Brennan | last post by:
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????.???????????????????...
0
1800
by: anonymous | last post by:
I cannot compile LiDIA mathematical library version 2.1pre5 with the gmp 4.1.2 multiprecision library because the gcc compiler ver 3.2.2 20030222 Red Hat Linux 9 complains about some structures in the gmp.h header file: /usr/local/include/gmp.h:2073: declaration of C function `std::ostream& operator<<(std::ostream&, const __mpq_struct*)'...
1
3406
by: Marc Cromme | last post by:
I would like to ask a question about (good ?) style and possibilities in mixing C FILE* and C++ file streams. The background is that I want to use the C libpng library from within C++, but I would like to open C++ file streams due to easier exception handeling and safe closure of file ressources. Question 1: I open a standard file...
13
2295
by: Jason Heyes | last post by:
Is this all it takes to copy a file in C++? Is there any error handling missing in this function? bool copy_file(std::string first, std::string second) { std::ifstream in(first.c_str()); if (!in.is_open()) return false; std::ofstream out(second.c_str());
0
1370
by: Kirk | last post by:
I have created an application that copies images I select to two printer que's. The images are either AutoCAD plot files (.PLT) or scanned images (.TIF). My method works well for the PLT files on my two printers, however, I have problems when copying TIF images to one of them. I do a simply copy of the files like this: File.Copy...
18
1785
by: panig | last post by:
how the program knows when a file is finsihed, mmm?
0
851
by: =?Utf-8?B?UmFqaXYgRGFz?= | last post by:
Hi, I am able to view the contents of a remote folder in explorer, but am unable to enumerate content through c# console app? What could be the cause and fix? foreach (String s in Directory.GetFiles(@"\\myshare\bin\")) { Console.WriteLine(s);
8
2912
by: Johnny | last post by:
Hi all: I have an ASP.NET form that reads an Excel file and populates a datagrid. If I load the form in IE on the server, and select a local file, the code works fine. However if I load the form in IE from a client (http://server/readexcel.aspx) and try to read a local Excel file, the file cannot be opened. I imagine it's some kind of a...
7
6615
by: sudhaMurugesan | last post by:
Hi all, I use asp.net with c#. I upload a file and save in a directory. I want to reopen the file so i check for file exists and then open it. But though the file exists, the file.exists(fullpath) returns false . can anyone help me ? My code is here... string paths = this.MapPath("../TerminateDoc/Excel/" + fileName.Trim()); /* paths...
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.