S
subramanian100in
The following program is ONLY for learning C++.
Consider the program x.cpp:
#include <cstdlib>
#include <iostream>
using namespace std;
template <typename Type>
inline ostream& operator<<(ostream& os, Type obj)
{
Type temp;
temp = obj;
return os;
}
int main()
{
int x = 100;
cout << x;
cout << endl;
cout << 10;
cout << endl;
cout << "Enter some integers: ";
cout << endl;
return EXIT_SUCCESS;
}
When I compile this program as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
I get compilation error for the line
cout << "Enter some integers: ";
Here are the actual compilation errors:
x.cpp: In function `int main()':
x.cpp:22: error: ambiguous overload for 'operator<<' in 'std::cout <<
"Enter some integers: "'
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/
bits/ostream.tcc:98: note: candidates are: std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>:
perator<<(bool) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/
bits/ostream.tcc:284: note: std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>:
perator<<(const void*)
[with _CharT = char, _Traits = std::char_traits<char>]
x.cpp:8: note: std:
stream& operator<<(std:
stream&,
Type) [with Type = const char*]
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/
bits/ostream.tcc:612: note: std::basic_ostream<char,
_Traits>& std:
perator<<(std::basic_ostream<char, _Traits>&, const
char*) [with _Traits = std::char_traits<char>]/usr/lib/gcc/i386-redhat-
linux/3.4.3/../../../../include/c++/3.4.3/bits/ostream.tcc:567:
note: std::basic_ostream<_CharT, _Traits>&
std:
perator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
[with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/
bits/ostream.tcc:534: note: std::basic_ostream<_CharT,
_Traits>& std:
perator<<(std::basic_ostream<_CharT, _Traits>&, const
_CharT*) [with _CharT = char, _Traits = std::char_traits<char>]
Question:
The ambiguity error is reported by g++ for the line
cout << "Enter some integers: ";
because this program has the function defined for the overloaded
operator<<() and the standard library also
defines it - multiple functions for operator<<() and hence the error.
But what I do not understand is that why similar ambiguity error is
not reported for the lines
cout << x;
cout << endl;
cout << 10;
Kindly explain.
Thanks
V.Subramanian
Consider the program x.cpp:
#include <cstdlib>
#include <iostream>
using namespace std;
template <typename Type>
inline ostream& operator<<(ostream& os, Type obj)
{
Type temp;
temp = obj;
return os;
}
int main()
{
int x = 100;
cout << x;
cout << endl;
cout << 10;
cout << endl;
cout << "Enter some integers: ";
cout << endl;
return EXIT_SUCCESS;
}
When I compile this program as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
I get compilation error for the line
cout << "Enter some integers: ";
Here are the actual compilation errors:
x.cpp: In function `int main()':
x.cpp:22: error: ambiguous overload for 'operator<<' in 'std::cout <<
"Enter some integers: "'
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/
bits/ostream.tcc:98: note: candidates are: std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>:
_CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/
bits/ostream.tcc:284: note: std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>:
[with _CharT = char, _Traits = std::char_traits<char>]
x.cpp:8: note: std:
Type) [with Type = const char*]
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/
bits/ostream.tcc:612: note: std::basic_ostream<char,
_Traits>& std:
char*) [with _Traits = std::char_traits<char>]/usr/lib/gcc/i386-redhat-
linux/3.4.3/../../../../include/c++/3.4.3/bits/ostream.tcc:567:
note: std::basic_ostream<_CharT, _Traits>&
std:
[with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/
bits/ostream.tcc:534: note: std::basic_ostream<_CharT,
_Traits>& std:
_CharT*) [with _CharT = char, _Traits = std::char_traits<char>]
Question:
The ambiguity error is reported by g++ for the line
cout << "Enter some integers: ";
because this program has the function defined for the overloaded
operator<<() and the standard library also
defines it - multiple functions for operator<<() and hence the error.
But what I do not understand is that why similar ambiguity error is
not reported for the lines
cout << x;
cout << endl;
cout << 10;
Kindly explain.
Thanks
V.Subramanian