overload insert operator problem

This is a discussion on overload insert operator problem within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> //using namespace std; class SimpleClass { public: SimpleClass(); SimpleClass(SimpleClass &); ~SimpleClass(); friend ostream & operator<<(ostream &,SimpleClass&); int ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    overload insert operator problem

    Code:
    #include <iostream.h>
    //using namespace std;
    
    
    class SimpleClass
    {
    public:
    	SimpleClass();
    	SimpleClass(SimpleClass &);
    	~SimpleClass();
    	friend ostream & operator<<(ostream &,SimpleClass&);
    	int GetData();
    	void SetData(int);
    	void testfunction(SimpleClass);
    private:
    	int itsData;
    };
    
    SimpleClass::SimpleClass():itsData(0){cout<<"Default constructor called!"<<endl;}
    SimpleClass::SimpleClass(SimpleClass &rhs){cout<<"Copy constructor called!"<<endl;}
    SimpleClass::~SimpleClass(){cout<<"Destructor called!"<<endl;}
    
    int SimpleClass::GetData(){return itsData;}
     
    ostream& operator<<(ostream &o, SimpleClass &rhs)
    {
    	o<<rhs.GetData();
    	return o;
    }
    
    void SimpleClass::testfunction(SimpleClass obj)
    {	
    	//why at this place default constructor is not called
    	obj.SetData(2);
    }//Destructor is called when obj goes out of scope
    
    void SimpleClass::SetData(int i)
    {
    	itsData=i;
    }
    
    int main()
    {
    	SimpleClass obj; //this will cause message "Default constructor called"
    	cout<<obj<<endl;
    	obj.testfunction(obj);
    	return 0;
    }
    If I put on first two lines:
    #include <iostream>
    using namespace std;
    I get following compiler error:
    c:\program files\microsoft visual studio\myprojects\konstruktor\konstruktor.cpp(45) : error C2593: 'operator <<' is ambiguous
    Error executing cl.exe.

    but if I put
    #include <iostream.h> it works fine
    why is this happening and how to fix it

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    >> //why at this place default constructor is not called
    Because the copy constructor is called to make a copy of obj from main() into the obj from testfunction() parameter. If you don't want to make a copy, make the parameter a reference paramenter.

    >> I get following compiler error:
    Well I don't get that error using VC++ 6.0, but you could try:
    Code:
    friend ostream & operator<<(ostream &,const SimpleClass&);
    gg

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    My guess is that it is the MSVC friend bug. Try to download the approiate patch and the problem should be solved. If I remember it correctly you can avoid the bug by making
    Code:
    friend ostream & operator<<(ostream &,SimpleClass&);
    inline like this

    Code:
    class SimpleClass
    {
    public:
    	SimpleClass();
    	SimpleClass(SimpleClass &);
    	~SimpleClass();
    	friend ostream & operator<<(ostream &o,SimpleClass &rhs)
    {
    	o<<rhs.GetData();
    	return o;
    }
    	int GetData();
    	void SetData(int);
    	void testfunction(SimpleClass);
    private:
    	int itsData;
    };
    and remove the previous implementation. Skip this codesnippet
    Code:
    ostream& operator<<(ostream &o, SimpleClass &rhs)
    {
    	o<<rhs.GetData();
    	return o;
    }
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    You can download service pack 5 for 6.0 here.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. insert into binary tree question..
    By transgalactic2 in forum C Programming
    Replies: 32
    Last Post: 11-18-2008, 02:21 PM
  2. How to insert an item to a ListView
    By ysabelle in forum C++ Programming
    Replies: 2
    Last Post: 05-07-2007, 07:03 AM
  3. Insert unique record into database
    By groorj in forum C Programming
    Replies: 4
    Last Post: 12-29-2004, 10:06 AM
  4. Having trouble with operator*=()
    By Lurker in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2003, 02:03 PM
  5. Replies: 1
    Last Post: 09-17-2001, 05:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21