Why is implicit type conversion not performed for user-defined type in the following source code?
An implicit type conversion to type A should occur on the commented line, but it did not happen and an error occurred on that line.
I would like to know the grammatical rules and solutions for this error.
#include <iostream>
using namespace std;
class A {
int v;
public:
A(int _v): v(_v) {}
operator int (void) const {
return v;
}
friend ostream& operator << (ostream& os, const A& s) {
return os << "A: " << s.v;
}
};
class B {
int x, y;
public:
B(int _x, int _y): x(_x), y(_y) {}
operator A(void) const {
return A(x+y);
}
};
int main(void)
{
B b(1,2);
cout << A(b) << endl;
cout << (A)b << endl;
cout << b << endl; // error --> why?
return 0;
}
--> Additional Question
Thanks for Sam Varshavchik's reply .
Defining class A as below solves the problem.
class A {
int v;
public:
A(int _v): v(_v) {}
operator int (void) const {
return v;
}
friend ostream& operator << (ostream& os, const A& s);
};
ostream& operator << (ostream& os, const A& s){
return os << "A: " << s.v;
}
//////////////
class A {
int v;
public:
A(int _v): v(_v) {}
operator int (void) const {
return v;
}
friend ostream& operator << (ostream& os, const A& s){
return os << "A: " << s.v;
}
};
ostream& operator << (ostream& os, const A& s);
Whether or not to declare "operator <<" as a friend doesn't seem to matter here. Since the function is a global function, it can be referenced in other classes or other funcitons. I think it matters whether the block defining the function is inside or outside class A. Why is function declaration necessary when using inner definition? I want to know the grammatical basis.