no operator ""<<"" matches these operands error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: no operator ""<<"" matches these operands error

  1. #1
    Join Date
    May 2013
    Posts
    11

    no operator ""<<"" matches these operands error

    I'm using the same type of write function for 3 classes. They all use the << operator. The Visual Studio error is giving me this error in my Date class but not in the other classes. I've been messing with this for a while and can't figure out what I may be missing. Can anybody help me?

    Date class where error occurs
    Code:
    #ifndef H_DATE
    #define H_DATE
    #include <iostream>
    #include <string>
    #include <list>
    
    using namespace std;
    
    
    class Date
    {
    public:
    	Date();
    	Date(int, int, int, int, int);
    	void setDate(int, int, int, int, int);
    	int getYear();
    	int getMonth();
    	int getDay();
    	int getHour();
    	int getMinutes();
    	void writeDate(fstream &);
    	
    
    private:
    	int year;
    	int month;
    	int day;
    	int hour;
    	int minutes;
    
    };
    #endif
    
    #include <iostream>
    #include <string>
    #include <list>
    #include <iomanip>
    #include "date.h"
    using namespace std;
    
    
    
    Date::Date()
    {
    	setDate(0, 0, 0, 0, 0);
    }
    
    Date::Date(int y, int m, int d, int h, int min)
    	{
    		setDate(y, m, d, h, min);
    	}
    
    	void Date::setDate(int y, int m, int d, int h, int min)
    	{
    		 year = y;
    		 month = m;
    		 day = d;
    		 hour = h;
    		 minutes = min;
    	}
    
    	int Date::getYear()
    	{      return year;          }
    
    	int Date::getMonth()
    	{        return month;       }
    	 
    	int Date::getDay()
    	{         return day;        }
    
    	int Date::getHour()
    	{          return hour;      }
    
    	int Date::getMinutes()
    	{      return minutes;       }
    
    	void Date::writeDate(fstream &outfile)
    	{
    		outfile << year << " ";        //each line in this function gives me the error
    		outfile << month << " ";
    		outfile << day << endl;
    		outfile << hour << " ";
    		outfile << minutes << endl;
    	}

    Here is the Item class function that calls it and it works fine without the dates being printed
    Code:
    #ifndef H_ITEMS
    #define H_ITEMS
    #include <iostream>
    #include <string>
    #include <list>
    #include <queue>
    #include <iomanip>
    #include "customers.h"
    #include "date.h"
    #include "bids.h"
    using namespace std;
    
    class Item
    {
    public:
    	Item();
    	Item(int, int, int, int, int, int, int, 
    		int, int, int, int, int, string, double);
    	void setItem(int, int, int, int, int, int, int, 
    		int, int, int, int, int, string, double, double, bool);
    	int getItemIDd();
    	string getItemName();
    	double getPrice();
    	bool getisSold();
    	double getTotalCommission();
    	double getCommission();
    	double getHighestBid();
    
    	void AddtoBids(Bid &);
    	void itemSold(const double COM_PER);
    	void findItem(int, bool &);
    	bool BidisEmpty();
    	void CalculateTotalCommission();
    
    	void printCustomersItems();
    	void printItemsForSale();
    	void printItemsSold();
    	void printItemsAndBiddingList();
    	void isCustomerItem(int, bool &);
    	void writeItem(fstream &);
    
    private:
    	int itemID;
    	int cID;
    	string itemName;
    	double openingPrice;
    	bool isSold;
    	Date listDate;
    	Date endDate;
    	double commission;
    
    	static double totalCommission;
    	priority_queue <Bid> bidQueue;
    	vector <Bid>bidVec;
    };
    #endif
    
    #include <iostream>
    #include <string>
    #include <list>
    #include <queue>
    #include <vector>
    #include <iomanip>
    #include "customers.h"
    #include "date.h"
    #include "items.h"
    #include "bids.h"
    using namespace std;
    
    Item::Item()
    {
    	itemID = 0;
    	cID = 0;
    	itemName = " ";
    	openingPrice = 0;
    	isSold = false;
    	commission = 0;
    	listDate.setDate(0, 0, 0, 0, 0);
    	endDate.setDate(0, 0, 0, 0, 0);
    }
    
    Item::Item(int y, int m, int d, int h, int min, int ey, int em, int ed, int eh, int emin,
    	int id, int cId, string i, double p):
    	listDate(y, m, h, d, min), endDate(ey, em, ed, eh, emin)
    {
    
    	itemID = id;
    	cID = cId;
    	itemName = i;
    	openingPrice = p;
    	isSold = false;
    	commission = 0;
    }
    
    
    void Item::setItem(int ly, int lm, int ld, int lh, int lmin, 
    	int ey, int em, int ed, int eh, int emin, int id, int cId, string i, double p, double c, bool s)
    {
    	itemID = id;
    	cID = cId;
    	itemName = i;
    	openingPrice = p;
    	listDate.setDate(ly, lm, ld, lh, lmin);
    	isSold = s;
    	commission = c;
    	cout << "set Item commission " << commission << endl;
    	endDate.setDate(ey, em, ed, eh, emin);
    }
    
    void Item::AddtoBids(Bid &temp)
    {
    	cout << temp.getItID() << " " << temp.getBid() << endl;
    	bidQueue.push(temp);
    	bidVec.push_back(temp);
    }
    
      
    
    void Item::itemSold(const double COM_PER)
    {
    		isSold = true;
    		Bid sTemp = bidQueue.top();
    		commission = COM_PER * sTemp.getBid();
    }
    
    bool Item::BidisEmpty()
    {        return (bidQueue.empty());         }
    
    
    
    void Item::CalculateTotalCommission()
    {
    	totalCommission += commission;
    	cout << "totalCommission " << totalCommission << endl;
    }
    
    
    void Item::findItem(int id, bool &found) 
    {
    	found = false;   
    	if (this->itemID == id)
    		found = true;
    	
    }
    
     
    void Item::isCustomerItem(int cid, bool &found) 
    {
    	found = false;   
    	if (this->cID == cid)
    		found = true;
    	
    }
    
    
    int Item::getItemIDd()
    {       return itemID;           }
    
    string Item::getItemName()
    {       return itemName;         }
    
    double Item::getPrice()
    {      return openingPrice;      }
    
    bool Item::getisSold()
    {        return isSold;         }
    
    double Item::getCommission()
    {      return commission;      }
    
    double Item::getHighestBid()
    {   
    	Bid sTemp = bidQueue.top();
    	return sTemp.getBid();
    }
    
    double Item::getTotalCommission()
    {       return totalCommission;           }
    
    
    void Item::printItemsForSale()
    {
    	cout << "Item ID: " << itemID << endl;
    	cout << "Name of item: " << itemName << endl;
    	cout << fixed << setprecision(2);
    	cout << "Opening Price: $" << openingPrice << endl;
    	if (!bidQueue.empty())
    	{
    		Bid sTemp = bidQueue.top();
    		cout << "Current highest bid: $" << sTemp.getBid() << endl;
    	}
    	else
    		cout << "No current bids.\n";
    	cout << endl;
    	
    }
    
    void Item::printCustomersItems()
    {
    	cout << "Item ID: " << itemID << endl;
    	cout << "Name of item: " << itemName << endl;
    	cout << "Opening bid price: $" << openingPrice << endl;
    	cout << endl;
    }
    
    
    void Item::printItemsSold()
    {
    	Bid sTemp = bidQueue.top();
    	cout << "Item ID: " << itemID << endl;
    	cout << "Name of item: " << itemName << endl;
    	cout << fixed << setprecision(2);
    	cout << "Winning bid: $" << sTemp.getBid() << endl;
    	cout << "Commission: $" << commission << endl;
    	cout << fixed << setprecision(2);
    	cout << "Listing Date: " << listDate.getMonth() << "/" <<
    		listDate.getDay() << "/" << listDate.getYear() << " ";
    
    	cout  << "Time: ";
    	if (listDate.getHour() > 12)
    		cout << (listDate.getHour() - 12) << ":" <<
    			listDate.getMinutes() << "PM\n";
    	else
    		cout << listDate.getHour() << ":" <<
    			listDate.getMinutes() << "AM\n";
    
    	cout << "Sold by Date: " << endDate.getMonth() << "/" <<
    		endDate.getDay() << "/" << endDate.getYear() << " ";
    	cout  << "Time: ";
    	if (endDate.getHour() > 12)
    		cout << (endDate.getHour() - 12) << ":" <<
    			endDate.getMinutes() << "PM\n";
    	else
    		cout << endDate.getHour() << ":" <<
    			endDate.getMinutes() << "AM\n";
    	cout << endl;
    
    }
    
    void Item::printItemsAndBiddingList()
    {
    		cout << "Item ID: " << itemID << endl;
    		cout << "Name of item: " << itemName << endl;
    		cout << "Cut-off Date: " << endDate.getMonth() << "/" <<
    			endDate.getDay() << "/" << endDate.getYear() << " ";
    		cout  << "Cut-off Time: ";
    		if (endDate.getHour() > 12)
    			cout << (endDate.getHour() - 12) << ":" <<
    				endDate.getMinutes() << "PM\n";
    		else
    			cout << endDate.getHour() << ":" <<
    				endDate.getMinutes() << "AM\n";
    
    		if (isSold)
    			cout << "Item has been sold. The past bids are:\n";
    		if(!isSold)
    		{
    			if (BidisEmpty())
    				cout << "There are no bids on the item.\n\n";
    		}
    		else
    			cout << "Item is still up for bidding. Current bids are:\n";
    
    		if (!BidisEmpty())
    		{
    			do
    			{
    				Bid stemp = bidQueue.top();
    				cout << "Bid " << bidQueue.size() << " is $" << stemp;
    				bidQueue.pop();
    			}while (!BidisEmpty());
    			cout << endl;
    		}
    		// repopulate the priority queue
    		for (int i=0; i < bidVec.size(); i++)
    			bidQueue.push(bidVec[i]);
    }
    
    void Item::writeItem(fstream &ifile)
    {
    	ifile << cID << endl;
    	ifile << itemID << endl;
    	ifile << itemName << endl;
    	ifile << fixed << setprecision(2) << openingPrice << endl;
    	listDate.writeDate(ifile);
    	ifile << isSold << endl;
    	endDate.writeDate(ifile);
    	ifile << fixed << setprecision(2) << commission << endl;
    
    }
    I also have a Customer class that works fine on its own.

    Here is the statement in main that calls the Item write function. It is also working fine.
    Code:
    	   //write the items file
        itemfile.open("bidItems.txt", ios::out);
        if (!itemfile)
        {
            cout << "The items for bidding file will not open for writing. "
                 << "This program will terminate." << endl;
            return 1;
        }
    
    	for (i = itemList.begin(); i!=itemList.end(); ++i)
    	{
    		cout << "Writing item: " << i->getItemIDd() << endl;
    		i->writeItem(itemfile);
    	}

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,355

    Re: no operator ""<<"" matches these operands error

    Instead of declaring the member function as:
    void writeDate(fstream &);
    I suggest that you declare it as:
    Code:
    void writeDate(std::ostream& out) const;
    Then in the header:
    Code:
    #include <iosfwd>
    In the source file:
    Code:
    #include <ostream>
    You do not need to #include <iostream> unless you want to use std::cin, std::cout and/or related pre-defined I/O stream objects.

    Also, you should not have using directives like using namespace std; at file scope in header files. Fully qualify those names in the header file, moving the using directive to the source file (after the header inclusions) if you wish.

    Incidentally, instead of naming that member function writeDate, you might want to consider overloading operator << instead, i.e., declare a non-member function, possibly as a friend:
    Code:
    std::ostream& operator<<(std::ostream& out, const Date& date);
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2013
    Posts
    11

    Re: no operator ""<<"" matches these operands error

    Thanks for the suggestion. I'll consider doing it but what I really want to know is - why is the operator << being treated differently in one class than it is in the other two classes. It works great in Item and Customer.

  4. #4
    Join Date
    May 2013
    Posts
    11

    Re: no operator ""<<"" matches these operands error

    Oh, I do need iostream since I had the file open for input earlier in the program. It was closed before it got opened again.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,355

    Re: no operator ""<<"" matches these operands error

    Quote Originally Posted by luvCats
    why is the operator << being treated differently in one class than it is in the other two classes. It works great in Item and Customer.
    Take a look at your list of header inclusions in the header containing the definition of Date:
    Code:
    #include <iostream>
    #include <string>
    #include <list>
    You failed to #include <fstream>, yet in that file you wrote:
    Code:
    void writeDate(fstream &);
    This happened for the other headers too, but perhaps by chance some other header that you included happened to include <fstream>, directly or indirectly.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    May 2013
    Posts
    11

    Re: no operator ""<<"" matches these operands error

    That did it!!! Thank you! Thank you! Thank you!

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,355

    Re: no operator ""<<"" matches these operands error

    I hope that you take my other suggestions instead of just putting #include <fstream> as a band-aid. There is no need to constrain the output to be to a std::fstream in this case. Furthermore, notice that I made your function const correct. The using directive thing should be fixed.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center