ofstream() '<<' ambiguous error

Pages: 12

Cannot satisfy the Compiler with this simple code.
It works just fine in another Xcode C++ project, same Build Settings,
I can cout << ss ; OK, but cannot output to a file?

#include <iostream>
#include <fstream>
std::string ss; // content is successfully created by the program.
using namespace std;
ofstream out("/Users/rmcinnes/Downloads/OutFile.txt");
out << ss ;
out.close();

This code fails even if I substitute "plain text" for the string ss.

Use of overloaded operator '<<' is ambiguous (with operand types 'std::__1::ofstream' (aka 'basic_ofstream<char>') and 'std::string' (aka 'basic_string<char>'))
Last edited on
Show the whole code (or, better, enough runnable code to see the problem).
You need to #include <string>
Coder777
Thank you for your response, but unfortunately,
adding #include <string> did not resolve the issue.
lastchance
The code works just fine as a test code in another program...
But not an add in to my main application...
That is what has me puzzled.

Then post the whole code. Sometimes a compile issue is due to an earlier problem.
Hello rjmcinnes,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting and line spacing it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

*** I found the second link to be the most help. ***

Revised 08/16/2021


As lastchance said it is best to post enough code that can be compiled and run that demonstrates the problem. Also it is best to post the error message that the compiler gives you and not what you think it means.

You may get by with std::string ss; and not have the compiler complain, but when you get to out << ss; "cout" or "out" do not know how to process the string with out the header file.

Next question is, ofstream out("/Users/rmcinnes/Downloads/OutFile.txt");, but how do you know if it is open and ready to use? Just because an "ofstream" will create the file if needed does not mean that it will. You could have a problem with the path that keeps the file from opening or being created.

Since you did not mention what operating system, IDE/compiler you are using I can only guess that it is a Mac.

I am not familiar with how a Mac uses a path or if there is anything missing in what you posted, but compared to Windows which needs a path that starts with a drive letter, (C:/...), your path may not be correct. Just a guess.

Andy
rjmcinnes wrote:
The code works just fine as a test code in another program...
But not an add in to my main application...
That is what has me puzzled.


But since we haven't got a view on your computer screen we don't know what is in either your test code or the one you are puzzled about.

So, to minimise puzzledom, please post your code.
The code you posted works just fine in VS2019.
Please post code that reproduces the problem (using code tags).

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	std::string ss; // content is successfully created by the program.	
	ss = "Some content";
	ofstream out("OutFile.txt");
	out << ss;
	out.close();
}


edit: The first time I looked at your code, I misread it. I saw the ss and thought you were using a stringstream. The << operator is used to insert output into the stringstream. streamstream does not support output to an ostream.
Last edited on
AbstractionAnon,
Thanks, confirmed, the code works fine for me too, in Apple Xcode 12.5.1,
C++ GNU++14 (-std=gnu++14),
libc++(LLVM C++ stand library withC++11 support).
But is does not work when added to my other program.

SeePlus,
As you point out, the compile issue must be related to an earlier problem in my other program, causing the output code, when imbedded to misbehave.
That other program is too large to post here in its entirety, so I will have to consider an incremental rebuild of that code, to see if I can spot the conflict.

Handy Andy,
Confirmed, it is on a Mac, created as a macOS Xcode Command Line Tool Project, Language C++,
Build settings:
Clang Code Generation: Debug Information, Compiler default,
Clang C Language Dialect: gnu11,
Clang C++ Language Dialect: GNU14 [-std=gnu14++]
C++ Standard Library: libc++ (LLVM C++ standard library with C++ support)

And I have confirmed the full path name is valid, the file is created, and is open.
I inserted this code, which I assume does confirm the file is open and OK.
1
2
    if (out.is_open())     // Code to ensure file is properly created, and healthy.
        if (out.good())


To all:
To clarify, this is the error I see in my other program:
Use of overloaded operator '<<' is ambiguous (with operand types 'std::__1::ofstream' (aka 'basic_ofstream<char>') and 'std::string' (aka 'basic_string<char>'))
To all:
Thank you all, for your prompt and helpful support, I am very grateful.
This morning, I went back to basics, and behold, this simple approach works just fine...
1
2
3
4
    FILE *fp;
    fp=fopen("/Users/rmcinnes/Downloads/PiOutFile.txt","w");
    fprintf(fp,"%s",ss.c_str());
    fclose(fp);


It works so well, I have decided to abandon trying to figure out why this ofstream code does not.
1
2
3
    ofstream out("/Users/rmcinnes/Downloads/PiOutFile.txt");
    out << ss ;
    out.close();
Last edited on
That is a pointless decision - a backward step, which prevents you using the abstraction of streams and denying the existence of a problem in your code. Just because you are unwilling to share code.

At a rough guess you have overloaded the operator << for some class of your own and done so incorrectly.
LastChance,
I understand your concerns, and I would really have liked to have got this issue sorted, however...

The C++ code of my app that is seeing this issue is 450 lines.
This program deploys an Abitrary Precision C++ Package: http://www.hvks.com/Numerical/arbitrary_precision.html,
courtesy of Henrik Vestermark.
The three programs that I have adopted from that package are 5,800, 2,200, and 2,260 lines of code.
In total, 10,710 lines, hardly practicable to post here.

But more importantly, if the Class defined in Henrik's package is the issue, as you suspect, I am hardly qualified to go poking around in Henrik's 10,260 line of proprietary code! without risk of messing something up.

Henrik Vestermark's code is free to download, should you wish to take a look through it?

Thanks for your interest and guidance.
For me, I have a solution, it works fine, that's all I am concerned about right now, to be able to save the 200MB result of a five day calculation to a text file.
Last edited on
Well, if you download the arbitrary precision library from that site (it's not that large in kB) then you can run the following test program which reproduces your error (and a few others).

You only need the header iprecision.h

I think some of the template functions in that code are asking for trouble.

Minimal test program:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>
#include <string>
#include "iprecision.h"

int main()
{
   std::string ss = "Help!";
   std::ofstream fout( "output" );
   std::cout << ss;
   fout << ss;
}


In file included from test.cpp:4:
iprecision.h: In constructor 'int_precision::int_precision(const char*)':
iprecision.h:374:8: error: 'strlen' was not declared in this scope
    if( strlen(str) == 0 )
        ^~~~~~
iprecision.h:374:8: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'?
iprecision.h:122:1:
+#include <cstring>
 using std::atoi; using std::strtoul;
iprecision.h:374:8:
    if( strlen(str) == 0 )
        ^~~~~~
test.cpp: In function 'int main()':
test.cpp:11:9: error: ambiguous overload for 'operator<<' (operand types are 'std::ofstream' {aka 'std::basic_ofstream<char>'} and 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'})
    fout << ss;
    ~~~~~^~~~~
In file included from test.cpp:4:
iprecision.h:1865:43: note: candidate: 'int_precision operator<<(const _Ty&, const int_precision&) [with _Ty = std::basic_ofstream<char>]'
 template <class _Ty> inline int_precision operator<<(  const _Ty& lhs, const int_precision& rhs )
                                           ^~~~~~~~
In file included from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/string:52,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/locale_classes.h:40,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/ios_base.h:41,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:42,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
                 from test.cpp:1:
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/basic_string.h:6284:5: note: candidate: 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
     operator<<(basic_ostream<_CharT, _Traits>& __os,
     ^~~~~~~~
In file included from test.cpp:4:
iprecision.h: At global scope:
iprecision.h:107:13: warning: '_VI_' defined but not used [-Wunused-variable]
 static char _VI_[] = "@(#)iprecision.h 02.08 -- Copyright (C) Henrik Vestermark";
Last edited on
Yes. The problem is on line 1865 in iprecision.h - where an incorrect template definition overload for operator<< is defined. There's also problems with other templated operator overloads!

@rjmcinnes,

This is a minimal example that will reproduce your problem.

The routines you are using are careless in the extreme with template functions, and are lining up to confuse bit-shifting (which appears to be their intention) with stream operations.

The word "explicit" in front of the int_precision constructor would avoid the unintentional conversion. However, plenty more might break.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>
#include <string>


class int_precision
{
public:
   int_precision( const std::string& str ){}
};


template <class _Ty> int_precision operator << ( const _Ty& lhs, const int_precision& rhs )
{
   return int_precision( lhs );     // (The original involved more functions and rhs)
}


int main()
{
   std::string ss = "Help!";
   std::ofstream fout( "output" );
   std::cout << ss;
   fout << ss;
}


 In function 'int main()':
24:9: error: ambiguous overload for 'operator<<' (operand types are 'std::ofstream {aka std::basic_ofstream<char>}' and 'std::string {aka std::basic_string<char>}')
24:9: note: candidates are:
13:36: note: int_precision operator<<(const _Ty&, const int_precision&) [with _Ty = std::basic_ofstream<char>]
In file included from /usr/include/c++/4.9/string:52:0,
                 from /usr/include/c++/4.9/bits/locale_classes.h:40,
                 from /usr/include/c++/4.9/bits/ios_base.h:41,
                 from /usr/include/c++/4.9/ios:42,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from 1:
/usr/include/c++/4.9/bits/basic_string.h:2772:5: note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
     operator<<(basic_ostream<_CharT, _Traits>& __os,
     ^

Last edited on
LastChance, SeePlus,
Thank you for your tenacity, you guys are amazing, really...
I have passed your feedback on to the author of the Arbitrary Precision Math Package (Henrik Vestermark), who I am sure will very much appreciate your contribution to his work.
Robert.
An possible 'easy' solution is to put iprecision.h functions/classes et al into their own namespace - so that they don't interfere with the standard library ones.
I can understand why there is an ambiguous overload for std::ofstream. I don't quite understand why the same ambiguity does not exist for std::cout.

p.s. I'm sorry if this was answered earlier in the thread. If it's there, I didn't see it.
I don't know either, @doug4.

Hopefully, some other forum user can enlighten us ...
Pages: 12