Why cannot compiler find operator<<. Where is the compiler looking into to find the definition of operator<< when it encounters the line
cout <<f.some_func()<<endl;
The error:
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::vector<std::vector<int> >’)
cout <<f.some_func()<<endl;
some notes:....
error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
cout <<f.some_func()<<endl;
#include <iostream>
#include <string>
#include<vector>
using namespace std;
struct Bar
{
int y;
};
class Foo
{
int x;
friend ostream & operator<<(ostream & os, Foo& f)
{
os << "Foo: " << f.x << endl;
return os;
}
friend ostream & operator<<(ostream & os, Bar& b)
{
Foo f;
os << "Bar b.y: " << b.y << endl;
os << "Bar f.x: " << f.x << endl;
return os;
}
friend ostream & operator<<(ostream & os, vector<vector<int> > const& vec ){
os << 5;
return os;
}
public:
Foo(int x = 10):x{x}{}
vector<vector<int> > some_func(){
vector<vector<int> > abc(3, vector<int>(3));
int x = 900;
return abc;
}
//If I do this
void wrapper(){
cout << this->some_func() << endl;
}
};
int main()
{
Bar b { 1 };
Foo f{ 13 };
cout << f << endl;
//cout << b << endl;
cout <<f.some_func()<<endl;
return 0;
}
Functions like this that are defined as friends inside the class have rather unusual name lookup rules. They're free functions, but their names are inside of the class of which they're a friend. Because of this, they're only found via argument-dependent lookup.
For that to work, however, at least one of the arguments has to be something like a reference to an object of the class in which it's defined, or else something defined inside that class.
In your case, the arguments are an iostream and a vector<vector<int>>, neither of which has any relationship to Foo, so the compiler doesn't use argument-dependent lookup to find the function inside of Foo.
The obvious way to make it work is to have the overloads for Foo and Bar inside the definitions of Foo and Bar, and the overload for std::vector<vector<int>> outside any class:
#include <iostream>
#include <string>
#include<vector>
using namespace std;
struct Bar
{
int y;
friend ostream & operator<<(ostream & os, Bar& b)
{
return os << "Bar b.y: " << b.y;
}
};
class Foo
{
int x;
friend ostream & operator<<(ostream & os, Foo& f)
{
return os << "Foo: " << f.x;
}
public:
Foo(int x = 10) : x{ x } {}
vector<vector<int> > some_func() {
vector<vector<int> > abc(3, vector<int>(3));
int x = 900;
return abc;
}
};
ostream & operator<<(ostream & os, vector<vector<int> > vec) {
return os << 5;
}
int main()
{
Bar b{ 1 };
Foo f{ 13 };
cout << f << '\n';
cout << b << '\n';
cout << f.some_func() << '\n';
return 0;
}
Related
I'm trying to write a single function template instead of a bunch of similar overloads for the insertion operator. The redundant overloaded versions work, but when I try to unite them in a single function template, the compiler complains of ambiguity. For example:
#include <iostream>
#include <list>
class fooBar
{
public:
fooBar(int iVal): iValue(iVal) {}
int getValue() {return iValue;}
private:
int iValue;
};
class foo
{
public:
foo()
{
for(int i = 0; i < 64; i++)
lstFooBars.push_back(fooBar(i));
}
std::list<fooBar>& getList()
{
return lstFooBars;
}
private:
std::list<fooBar> lstFooBars;
};
class bar
{
public:
bar()
{
for(int i = 0; i < 64; i++)
lstFooBars.push_back(fooBar(i));
}
std::list<fooBar>& getList()
{
return lstFooBars;
}
private:
std::list<fooBar> lstFooBars;
};
std::ostream& operator<<(std::ostream& osOut, fooBar& fbrFooBar)
{
osOut << fbrFooBar.getValue();
return osOut;
}
template <typename T> std::ostream& operator<<(std::ostream& osOut, T& tContainer)
{
for(fooBar fbrFooBar: tContainer.getList())
osOut << "[" << fbrFooBar << "] ";
return osOut;
}
int main()
{
foo fooFoo;
bar barBar;
std::cout << std::endl << fooFoo << std::endl << std::endl;
std::cout << std::endl << barBar << std::endl << std::endl;
return 0;
}
...and the compiler tells me that:
test.cpp: In function ‘std::ostream& operator<<(std::ostream&, T&)’:
test.cpp:63:9: error: ambiguous overload for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const char [2]’)
63 | osOut << "[" << fbrFooBar << "] ";
| ~~~~~ ^~ ~~~
| | |
| | const char [2]
| std::ostream {aka std::basic_ostream<char>}
Why does it work when you overload the same function over and over for each case and it doesn't compile like this? What am I missing here?
You've inadvertedly added a possible overload for const char* by making this:
template<typename T>
std::ostream& operator<<(std::ostream& osOut, T& tContainer)
If you narrow it down a bit with SFINAE, it should work.
This overload will only work for types with a getList() member function for example:
template<typename T, typename U = decltype(std::declval<T>().getList())>
std::ostream& operator<<(std::ostream& osOut, T& tContainer)
operator<< by default takes chars as argument, not string literals (inside "s) https://www.cplusplus.com/reference/ostream/ostream/operator-free/.
So, in order to make the call in the code you provide not ambiguous, you should try either to use single chars, or std::string:
#include <iostream>
#include <list>
#include <string>
class fooBar
{
public:
fooBar(int iVal): iValue(iVal) {}
int getValue() {return iValue;}
private:
int iValue;
};
class foo
{
public:
foo()
{
for(int i = 0; i < 64; i++)
lstFooBars.push_back(fooBar(i));
}
std::list<fooBar>& getList()
{
return lstFooBars;
}
private:
std::list<fooBar> lstFooBars;
};
class bar
{
public:
bar()
{
for(int i = 0; i < 64; i++)
lstFooBars.push_back(fooBar(i));
}
std::list<fooBar>& getList()
{
return lstFooBars;
}
private:
std::list<fooBar> lstFooBars;
};
std::ostream& operator<<(std::ostream& osOut, fooBar& fbrFooBar)
{
osOut << fbrFooBar.getValue();
return osOut;
}
template <typename T> std::ostream& operator<<(std::ostream& osOut, T& tContainer)
{
for(fooBar fbrFooBar: tContainer.getList())
//osOut << std::string("[") << fbrFooBar << std::string("] "); // solution 1: use std::string
osOut << '[' << fbrFooBar << ']' << ' '; // solution 2: use single chars
return osOut;
}
int main()
{
foo fooFoo;
bar barBar;
std::cout << std::endl << fooFoo << std::endl << std::endl;
std::cout << std::endl << barBar << std::endl << std::endl;
return 0;
}
I have a problem with my code. I have two classes, A and B, and B inherits A. I also have operators << overloaded in both classes.
Everything works, I have no compiler errors, but it seems something is wrong. As far as I understand polymorphism, when I use pointers to base class while creating child class with new, calling a method should match the child class, not the base class.
For the code below,
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
A(int aa) : a(aa) {};
virtual void show(ostream& o) const
{
o << "a = " << a << "\n";
}
};
ostream& operator << (ostream& os, const A &o)
{
o.show(os);
return os;
}
class B : public A
{
private:
int b;
public:
B(int bb, int aa) : A(aa), b(bb){}
int getb() const {return b;}
};
ostream & operator << ( ostream & os, const B & o)
{
os << static_cast <const A &>(o);
os << "\n";
os << "b = " << o.getb() << "\n";
return os;
}
int main()
{
A *o1 = new B(2,3);
cout << *o1;
cout << "---------------------\n";
B *o2 = new B(2,3);
cout << *o2;
return 0;
}
In main:
A *o1 = new B(2,3);
cout << *o1;
Shows a = 3, instead of showing a = 3 b = 2 (the call should match the child class, not the base class). The thing is, I need to implement the << and >> operators in every child class, but I think they do not behave as they should.
The output of the program:
Even the modified code with re-implmented show method shows wrong results, it does not show a at all this time:
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
A(int aa) : a(aa) {};
virtual void show(ostream& o) const
{
o << "a = " << a << "\n";
}
};
ostream& operator << (ostream& os, const A &o)
{
o.show(os);
return os;
}
class B : public A
{
private:
int b;
public:
B(int bb, int aa) : A(aa), b(bb) {}
int getb() const
{
return b;
}
void show(ostream& o) const
{
o << "b = " << b << "\n";
}
};
ostream & operator << ( ostream & os, const B & o)
{
os << static_cast <const A &>(o);
o.show(os);
return os;
}
int main()
{
A *o1 = new B(2,3);
cout << *o1;
cout << "---------------------\n";
B *o2 = new B(2,3);
cout << *o2;
return 0;
}
enter image description here
you have to implement the virtual function show in derived class B:
class B: public A
{
public:
// some code here
virtual void show(ostream& o) const
{
o << "b = " << b << "\n";
}
};
when I use pointers to base class while creating child class with new,
calling a method should match the child class, not the base class
It does when you call a member function ("method" in some other languages), but operator<< is not a member function – it's an overloaded free function.
When choosing an overload, only the types known at compile-time are used.
Since o1 is an A*, *o1 is an A&, and the overload for A& is chosen.
You're doing this a bit "backwards"; you only need one operator<<, for the base class, which calls the virtual show, and then you override show in the derived classes.
Like this:
class A
{
// ...
virtual void show(ostream& o) const
{
o << "a = " << a << "\n";
}
};
ostream& operator << (ostream& os, const A &o)
{
o.show(os);
return os;
}
class B : public A
{
// ...
void show(ostream& o) const override
{
A::show(o); // Do the "A part".
o << "b = " << b << "\n";
}
};
Follow the same pattern for operator>>.
#include <iostream>
namespace outside {
struct A {
int outer = 42;
friend void print(A const& a, std::ostream& os)
{ os << "outside::A " << a.outer << '\n'; }
};
namespace inside {
struct A : outside::A {
int inner = 24;
void print(std::ostream& os) { } // Added for extra difficulty
friend void print(A const& a, std::ostream& os) {
// outside::A::print(a, os); // <- does not compile
os << " inside::A " << a.inner << '\n';
}
};
} // inside
} // outside
int main(int argc, char *argv[]) {
outside::A a_outside;
outside::inside::A a_inside;
print(a_outside, std::cout);
print(a_inside, std::cout);
}
Is there a way to qualify the print function so that both base and derived members are printed? I could move both the friend functions to their closest enclosing namespaces:
#include <iostream>
namespace outside {
struct A { int outer = 42; };
void print(A const& a, std::ostream& os)
{ os << "outside::A " << a.outer << '\n'; }
namespace inside {
struct A : outside::A {
void print(std::ostream& os) { } // Added for extra difficulty
int inner = 24;
};
void print(A const& a, std::ostream& os) {
outside::print(a, os); // <- works
os << " inside::A " << a.inner << '\n';
}
} // inside
} // outside
int main(int argc, char *argv[]) {
outside::A a_outside;
outside::inside::A a_inside;
print(a_outside, std::cout);
print(a_inside, std::cout);
}
This works, here's the result:
outside::A 42
outside::A 42
inside::A 24
Can the same be achieved with the friend functions though? Maybe using using?
EDIT: inside::A::print(std::ostream&) defeats the static cast suggest below, https://stackoverflow.com/a/22585103/710408. Any other options?
I find possible solution:
friend void print(A const& a, std::ostream& os) {
print(static_cast<const outside::A&>(a), os);
os << " inside::A " << a.inner << '\n';
}
Some info why this dont work: https://stackoverflow.com/a/382077/1938348
Conclusion is simple when you define friend function in class body its invisible unless you use ADL.
To made it work as you excepted you need change place of declaration:
namespace outside {
struct A {
int outer = 42;
friend void print(A const& a, std::ostream& os);
};
void print(A const& a, std::ostream& os) {
os << "outside::A " << a.outer << '\n';
}
namespace inside {
struct A : outside::A {
int inner = 24;
friend void print(A const& a, std::ostream& os);
};
void print(A const& a, std::ostream& os) {
outside::print(a, os);
os << " inside::A " << a.inner << '\n';
}
} // inside
} // outside
Do you mean:
friend void print(A const& a, std::ostream& os) {
// outside::A::print(a, os);
print(static_cast<const outside::A&>(a), os);
os << " inside::A " << a.inner << '\n';
}
I'd like to wrap std::cout for formatting, like so:
mycout([what type?] x, [optional args]) {
... // do some formatting on x first
std::cout << x;
}
and still be able to use expressive syntax like
mycout("test" << i << endl << somevar, indent)
instead of being forced to be more verbose like
mycout(std::stringstream("test") << i ...)
How can I implement this? What type to make x?
Edit: added consideration for optional arguments
How about this:
struct MyCout {};
extern MyCout myCout;
template <typename T>
MyCout& operator<< (MyCout &s, const T &x) {
//format x as you please
std::cout << x;
return s;
}
And put MyCout myCout; into any one .cpp file.
You can then use myCout like this:
myCout << "test" << x << std::endl;
And it will call the template operator<< which can do the formatting.
Of course, you can also provide overloads of the operator for special formatting of specific types if you want to.
EDIT
Apparently (thanks to #soon), for standard manipulators to work, a few more overloads are necessary:
MyCout& operator<< (MyCout &s, std::ostream& (*f)(std::ostream &)) {
f(std::cout);
return s;
}
MyCout& operator<< (MyCout &s, std::ostream& (*f)(std::ios &)) {
f(std::cout);
return s;
}
MyCout& operator<< (MyCout &s, std::ostream& (*f)(std::ios_base &)) {
f(std::cout);
return s;
}
EDIT 2
I may have misunderstoor your original requirements slightly. How about this (plus the same manipulator overloads as above):
struct MyCout
{
std::stringstream s;
template <typename T>
MyCout& operator << (const T &x) {
s << x;
return *this;
}
~MyCout() {
somehow_format(s);
std::cout << s.str();
}
};
int main() {
double y = 1.5;
MyCout() << "test" << y;
}
This comes easy with variadic template arguments:
template <class T>
void print(T t)
{
std::cout << t;
}
template <class T, class... Args>
void print(T t, Args... args)
{
std::cout << t << std::endl;
print(args...);
}
int main()
{
std::cout << std::boolalpha;
print(3, 's', true, false);
}
Output:
3
s
true
false
Live Demo
A variation from the answers:
#include <iostream>
using namespace std;
class MyCout
{
public:
MyCout& operator()(bool indent) {
if ( indent ) cout << '\t';
return *this;
}
template<class T>
MyCout& operator<<(T t) {
cout << t;
return *this;
}
MyCout& operator<<(ostream& (*f)(ostream& o)) {
cout << f;
return *this;
};
};
int main()
{
MyCout mycout;
int x = 10;
mycout(true)<< "test" << 2 << x << endl ;
}
You can use this kind of class:
#include <iostream>
using namespace std;
class CustomOut
{
public:
template<class T>
CustomOut& operator<<(const T& obj)
{
cout << " my-cout " << obj;
return *this;
}
};
int main()
{
CustomOut mycout;
mycout << "test" << 4 << "\n" << 3.4;
}
You'd need more code to use std::endl and other functors, so i've used here simple \n instead.
How do I overload the << operator for enums that are members of a class. Specifically, I have the following code below:
#include <iostream>
using namespace std;
namespace foo {
class bar {
public:
enum a { b, c, d};
static void print() {
cout << b << endl;
}
};
ostream& operator<< (ostream& os, bar::a var) {
switch (var) {
case bar::b:
return os << "b";
case bar::c:
return os << "c";
case bar::d:
return os << "d";
}
return os;
}
}
int main() {
foo::bar::print();
return 0;
}
How can I get the print function to print "b" instead of "1"?
Here is a simple solution :
#include <iostream>
using namespace std;
namespace foo {
class bar {
public:
enum a { b, c, d};
static void print();
};
ostream& operator<< (ostream& os, bar::a var) {
switch (var) {
case bar::b:
return os << "b";
case bar::c:
return os << "c";
case bar::d:
return os << "d";
}
return os;
}
void bar::print() {
cout << b << endl;
}
}
int main() {
foo::bar::print();
return 0;
}
[EDIT] As previously stated by aschepler, you only need to ensure that operator<<(ostream &, bar::a) is visible before the definition of bar::print.
class bar {
public:
enum a { b = 'b', c = 'c', d = 'd' };
static void print() {
cout << char(b) << endl;
}
};
The problem is that your use of cout << bar:: comes before your ostream<< bar:: overload is declared, so it's not calling your overload. Move the definition down.
class bar {
public:
enum a { b, c, d };
static void print();
};
ostream& operator<< (ostream& os, bar::a var) {
...
void bar::print()
{
cout << b << endl;
}
EDIT: I see some else posted that while I was typing this.