Thread: Operator overloading trouble

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    51

    Operator overloading trouble

    I can't seem to understand whats wrong with my program. I keep getting this linker error.

    Code:
    error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator
    <<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Bid_Class &)" (??6@YAAAV?$basic_ostream@DU?$ch
    ar_traits@D@std@@@std@@AAV01@AAVBid_Class@@@Z) referenced in function "class std::basic_ostream<char,struct std::ch
    ar_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,
    class Item_Class const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVItem_Class@@@Z)
    Code:
    //Private
    Bid_Class *bidPointer;
    //Public
    Bid_Class * getPointer() const { return bidPointer;}
    
    ostream& operator<< (ostream &out,const Item_Class& obj)
    {
    	Bid_Class *tempPointer=obj.getPointer();
    	out<<obj.getName()<<" ("<<obj.getID()<<")"<<endl;
    	if(obj.getBids()==0)
    	{
    		out<<"    *** No Current Bids ***"<<endl;
    	}
    	else
    	{
    		for(int i=0;i<obj.getBids();i++)
    		{
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    
    			out<<tempPointer[i];//This is the part that is giving me the linking errors
    
    //////////////////////////////////////////////////////////////////////////////////////////////////
    
    		}
    	}
    	return out;
    }
    
    //The operator that's being called.
    
    ostream& operator<<(ostream &out, const Bid_Class& obj)
    {
    	out<<"    --- "<<obj.bidDate.getMonth()<<'/'<<obj.bidDate.getDay()<<'/'<<obj.bidDate.getYear()<<' '<<obj.biddersName<<' '<<obj.amount;
    	return out;
    }
    This should work, but I keep getting some errors. Can anyone help?

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Looks like it's trying to reference a version of the overloaded stream operator whose 2nd parameter is class Bid_Class & and not class Bid_Class const &. So I think you are having issues with const and the compiler trying to link a different overloaded function than you think. Try declaring tempPointer as const.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Another possibility is that there is an ostream<<(ostream &, Bid_Class &obj) declared somewhere - note the missing const - but not implemented. Effectively that would mean you have told the compiler such a function exists (so the code compiles) and then the linker detects that you have lied.

    It is usually a good idea to provide small but complete samples of code that illustrate your concern. When you paraphrase your problem ("I think it's here, so that's all I show") it is easy to eliminate something relevant.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    Once I added const in front of the temporary pointer it suddenly started to work so thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C trouble with strtok and storing the values
    By robin2aj in forum C Programming
    Replies: 5
    Last Post: 03-18-2010, 06:52 AM
  2. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  3. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  4. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM