CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2011
    Location
    Washington D.C.
    Posts
    44

    Exclamation To Reach a member function of a class from ostream function(class's friend)

    Code:
    class Order  /* It is forbidden to change here*/
    {
    
    	private:
    
    		string orderID;
    		int orderDate;
    		string customerID;
    		string storeName;
    		vector<Product> shoppingList;
    
    	public:
    
    		Order();
    		Order(string orderID, int orderDate, string customerID, string
    		storeName);
    		~Order();
    		string getOrderID();
    		int getOrderDate();
    		string getCustomerID();
    		string getStoreName();
    		void setStore(string newStore);
    		void addProduct(Product p);
    		void removeProduct (string pname, int amount);
    		int orderTotal();
    		void listAllProducts();
    		void listProductsOfType (string t);
    		Product getProduct(int index);
    		int getProductSize();
    		int don(int a);
    		friend ostream& operator<<(ostream& out, const Order &order);
    };
    
    
    
    ostream& operator<<(ostream &out, const Order &order);
    
    
    
    ostream& operator<<(ostream &out, const Order &order)
    {
            int i;
                    out        <<order.orderID<<endl
                            <<order.orderDate<<endl
                            <<order.customerID<<endl
                            <<order.storeName<<endl
                            <<"Products:"<<endl;
    
    /*this         for (i=0 ; i<order.getProductSize() ; i++)
    /*does        {
    /*not work   out<< order.shoppingList[i] << endl;
                     }
    /*this       for (i=0 ; i<2 ; i++)
    /*works        {
    /*         out<< order.shoppingList[i] << endl;
                   }
    
    }
    when i call getProductSize function from main , there is no problem. HOwever, when i call getProductSize function(or any function) inside ostream function there is such a problem


    order.cpp: In function ‘std:stream& operator<<(std:stream&,
    const Order&)’:
    order.cpp:83: error: passing ‘const Order’ as
    ‘this’ argument of ‘int Order::getProductSize()’
    discards qualifiers

    Code:
    int Order::getProductSize()
    {
     return this->shoppingList.size();
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: To Reach a member function of a class from ostream function(class's friend)

    getProductSize() ought to be declared as a const function since it doesn't modify anything:
    Code:
    int getProductSize() const;
    I know you say you aren't allowed to modify anything, but check with your teacher whether this change is acceptable, since by rights that's how it ought to have been written from the start.

    In the meantime, you have two options.

    1) You can use a const_cast to de-const the order reference. This is not a good idea in general, but necessary at times when an interface such as this one is not const-correct:
    Code:
    for (i=0 ; i<const_cast<Order&>(order).getProductSize() ; i++)
    Or 2) you can call shoppingList.size() directly, since that's all getProductSize() does anyway.
    Code:
    for (i=0 ; i<order.shoppingList.size() ; i++)
    Note that this will probably generate a warning about comparison between a signed value (i) and an unsigned value (size()). To avoid this you can either static_cast the result of the size() call to int, or you can define i as an unsigned int. The latter is usually a better choice, although note that it can result in unexpected behavior when a loop is counting down to 0 rather than up from 0.

  3. #3
    Join Date
    Mar 2011
    Location
    Washington D.C.
    Posts
    44

    Re: To Reach a member function of a class from ostream function(class's friend)

    Ok. second option is worked. BUt now i want to reach shoppingList vector.It's type of productClass

    and Product class is
    Code:
    class Product
    {
    
    	private:
                    string productName;
    		string productType;
    		int productPrice;
    		int productAmount;
    
            public:
                      Product(string productName, string productType, int productPrice,int productAmount);
    /*
    
    
                     */
    }
    
    
    Product::Product(string productName, string productType, int productPrice,int productAmount)
    {
    	this->productName = productName;
    	this->productType = productType;
    	this->productPrice = productPrice;
    	this->productAmount = productAmount;
    }

    In my order class , as you know there is vector<product> shoppingList

    i added firstly products then add these products to my order class via shoppingList something like :
    Code:
            Product p1("apple", "fruit", 1,9 );// 1 is price  ,   9  is amount
    	Product p2("coke", "beverage", 10,50 );  10 is price ,    50  is amount
    	Product p3("cheese", "food", 3,10 );   3 is price ,    10  is amount
    
            Order o1("order1", 20071010, "cust1", "migros");
            o1.addProduct(p1);
    	o1.addProduct(p2);
    	o1.addProduct(p3);
    
            o2.removeProduct("coke",25);/* i want to reach coke via vectors and then subtract 25 from amount. But how can i reach coke via vector??*/
    
    THanks for advance
    Moreover
    THis is my addProduct function
    Code:
    void Order::addProduct(Product p)
    {
    shoppingList.push_back(p);
    }
    Last edited by loves_oi; October 20th, 2011 at 09:04 AM.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: To Reach a member function of a class from ostream function(class's friend)

    You'll simply need to iterate through the vector and check each product to see if it is called "coke". There are other containers (such as maps) which are better suited to this sort of thing than vectors, of course.

    The fact that those fields are private may be a problem. Are there public accessors?

  5. #5
    Join Date
    Mar 2011
    Location
    Washington D.C.
    Posts
    44

    Re: To Reach a member function of a class from ostream function(class's friend)

    no,there arent. all is mutator

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: To Reach a member function of a class from ostream function(class's friend)

    That seems unlikely. Show the complete Product header.

  7. #7
    Join Date
    Mar 2011
    Location
    Washington D.C.
    Posts
    44

    Re: To Reach a member function of a class from ostream function(class's friend)

    Quote Originally Posted by Lindley View Post
    That seems unlikely. Show the complete Product header.
    This is the header and it's forbidden to make any change in header.
    Code:
    
    class Product
    {
    
    	private:
    
    		string productName;
    		string productType;
    		int productPrice;
    		int productAmount;
    
    	public:
    
    		Product(string productName, string productType, int productPrice,int productAmount);
    		Product();
    		~Product();
    		string getProductName();
    		string getProductType();
    		int getProductPrice();
    		int getProductAmount();
    		void setAmount(int newAmount);
    		void setPrice(int newPrice);
    		friend ostream& operator<<(ostream& out, const Product &product);
    };
    
    
    
    ostream& operator<<(ostream &out, const Product &product);
    /* Order */
    
    
    
    class Order
    {
    
    	private:
    
    		string orderID;
    		int orderDate;
    		string customerID;
    		string storeName;
    		vector<Product> shoppingList;
    
    	public:
    
    		Order();
    		Order(string orderID, int orderDate, string customerID, string
    		storeName);
    		~Order();
    		string getOrderID();
    		int getOrderDate();
    		string getCustomerID();
    		string getStoreName();
    		void setStore(string newStore);
    		void addProduct(Product p);
    		void removeProduct (string pname, int amount);
    		int orderTotal();
    		void listAllProducts();
    		void listProductsOfType (string t);
    		Product getProduct(int index);
    		int getProductSize();
    		friend ostream& operator<<(ostream& out, const Order &order);
    };
    
    
    
    ostream& operator<<(ostream &out, const Order &order);
    /* Store */

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: To Reach a member function of a class from ostream function(class's friend)

    Okay, so just use getProductName() on each Product object to check if it is "coke" (or whatever string is specified). You can then use getProductAmount and setProductAmount() together with the delta to modify how many you have.

    It should be an error to end up with a negative amount of anything.

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