CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2008
    Posts
    5

    virtual class with friend fuctions

    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?

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class A {
    public:
    	string name;
    	A(string sName) {
    		name = sName;
    	}
    	virtual void mission();
    	string getName() {
    		return name;
    	}
    	friend ostream& operator << (ostream& out, A& oA) {
    		out << oA.getName() << endl;
    		return out;
    	}
    };
    
    class B : public A {
    public:
    	B() : A("bob") {
    		name = "bob";
    	}
    };
    
    int main() {
    
    	B DUDE;
    
    	cout << DUDE;
            // this doesnt work either
            cout << (A)DUDE;
    
    	return 0;
    }

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: virtual class with friend fuctions

    Your code won't link (and thus it won't run) because it cannot find an implementation of the mission member function. If you implement that one (or remove the declaration) it should link just fine.

    - petter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured