| loveless (56) | |||||
|
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? the header file (test.h) is the following code:
the main program is as follow:
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? thanks | |||||
|
|
|||||
| kbw (4336) | |
ostream& operator<<(ostream out, test &x) should beostream& 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). | |
|
|
|
| loveless (56) | |
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?? | |
|
|
|
| kbw (4336) | |
Did you change the friend declaration too? friend ostream& operator<<(ostream& out, const test &x); | |
|
|
|
| loveless (56) | |||
|
ya i've changed the function prototype and the function
| |||
|
|
|||
| jsmith (5804) | |||
You must pass the stream by reference, not by value. streams are not copyable
| |||
|
|
|||
| loveless (56) | |
| oo i see i go try it out | |
|
|
|
| loveless (56) | |
|
=.=''' i still got the same problem my compiler said: 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' | |
|
|
|
| lloydchristmas759 (71) | |
|
Add #include <ostream> at the beginning of your test.h file, and use std::ostream instead of ostream. | |
|
|
|
| loveless (56) | |
??then do i need to delete the friend ostream& operator<<(ostream out, const test &x); in the class member?
| |
|
|
|
| van (6) | |
| You should put the specification of the class in .h file and the implementation in .cpp file. | |
|
|
|
| van (6) | |
| Don't delet. Friend function is not a memeber of a class, but its declaration must be included in the class specification. | |
|
|
|
| loveless (56) | |||
|
=.=''' 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
| |||
|
|
|||
| van (6) | |||||
|
have you done this way? .h file
.cpp file
and also don't forget to include necessary libraries. | |||||
|
|
|||||
| loveless (56) | |
|
ya i've tried already but my compiler is still saying the same error by the way, the compiler i'm using is code block | |
|
|
|
| lloydchristmas759 (71) | ||
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. | ||
|
|
||
| loveless (56) | |||||
|
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
| |||||
|
|
|||||
| jysonia (4) | |
|
Another way to do is to add following lines in hpp file(new_file.hpp): #include <iostream> using namespace std ; <CLASS DECLARATION> CPP FILE will look like :- #include <iostream> #include "new_file.hpp" <CLASS DEFINITION> main() { ..... } Thanks JySonia | |
|
|
|