View Single Post
Join Date: Oct 2008
Posts: 2
Reputation: mbayabo is an unknown quantity at this point 
Solved Threads: 0
mbayabo mbayabo is offline Offline
Newbie Poster

virtual class with friend functions

 
0
  #1
Dec 1st, 2008
this is a similar example to what i have. it's simplified but it still get's the same error.

i need to be able to use << operator to print out all information from class A. i have no idea how to do that. i tried casting it as an A object but then it starts telling me that i can do that with virtual functions. is there any way to go around this?

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class A {
  6. public:
  7. string name;
  8. A(string sName) {
  9. name = sName;
  10. }
  11. virtual void mission();
  12. string getName() {
  13. return name;
  14. }
  15. friend ostream& operator << (ostream& out, A& oA) {
  16. out << oA.getName() << endl;
  17. return out;
  18. }
  19. };
  20.  
  21. class B : public A {
  22. public:
  23. B() : A("bob") {
  24. name = "bob";
  25. }
  26. };
  27.  
  28. int main() {
  29.  
  30. B DUDE;
  31.  
  32. cout << DUDE;
  33. // this doesnt work either
  34. cout << (A)DUDE;
  35.  
  36. return 0;
  37. }
Reply With Quote