hi everyone
i had a problem when implementing the friend function
why i can't compile the program when the friend function is written in the header files?
but i can successfully compile the program if i write the class function in the main program?
class test
{
public:
int a, b;
test();
test(int x, int y);
friend ostream& operator<<(ostream out, test &x);
};
test::test()
{
a = b = 0;
}
test::test(int x, int y)
{
a = x;
b = y;
}
ostream& operator<<(ostream out, test &x)
{
out << "a = " << x.a << endl;
out << "b = " << x.b << endl;
return out;
}
the main program is as follow:
1 2 3 4 5 6 7 8 9 10 11 12
#include<iostream>
#include"test.h"
usingnamespace std;
int main()
{
test a(3, 5);
test b(4, 8);
cout << a << b << endl;
}
can someone tell me if i am doing some mistake when implementing the friend function?
or is that i can't implement the friend function in the header file?
ostream& operator<<(ostream out, test &x) should be ostream& operator<<(ostream& out, const test &x)
If you only have one source file, it won't make a difference. If you have more than one, you'd expect it to compile but fail at link time with multiple definitions of ostream& operator<<(ostream& out, const test &x).
just now i've tried to add the const but my compiler still can't compile it correctly?
why does it still can't compile?
is there something wrong with the source code??
class test
{
public:
int a, b;
test();
test(int x, int y);
friend ostream& operator<<(ostream out, const test &x);
};
test::test()
{
a = b = 0;
}
test::test(int x, int y)
{
a = x;
b = y;
}
ostream& operator<<(ostream out, const test &x)
{
out << "a = " << x.a << endl;
out << "b = " << x.b << endl;
return out;
}
line 8 : 'ostream' is neither function nor member function; cannot be declared friend
line 8 : expected ';' before '&' token
line 22: expected constructor, destructor, or type conversion before '&' token
line 23: missing terminating " character
line 11: no match for 'operator<<' in 'std::cout << a'
=.=''' i've tried to implement the .h file into the .cpp file but it still shows the same error
but if i tried to implement the class into the main program it compiles and works fine
#include<iostream>
usingnamespace std;
class test
{
public:
int a, b;
test();
test(int x, int y);
friend ostream& operator<<( ostream& out, const test& x );
};
test::test()
{
a = b = 0;
}
test::test(int x, int y)
{
a = x;
b = y;
}
ostream& operator<<( ostream& out, const test& x ) {
return out << "a = " << x.a << std::endl << "b = " << x.b << std::endl;
}
int main()
{
test a(3, 5);
test b(4, 8);
cout << a << b << endl;
}
//this works fine
//but if i separate the class to the .h file my compiler will said error
i've tried to implement the .h file into the .cpp file but it still shows the same error
but if i tried to implement the class into the main program it compiles and works fine
Which is why I told you to replace ostream by std::ostream in your .h file....
To Van: Please don't confuse people. What you propose can obviously not compile as is.
o i see
now i can compile it successfully
special thanks lloydchristmas
and thanks to everyone that has help me to understand how to use friend function