double post
sorry
This is a discussion on help with classes within the C++ Programming forums, part of the General Programming Boards category; double post sorry...
double post
sorry
Last edited by ammadeus; 11-25-2006 at 04:22 PM.
Hi,
This is the first of possibly many posts to follow.
I must write this tool centered around 3 classes (product, order, stock).
The following questions are about the first 2 classes.
First and foremost the code
product.h
product.ccCode:#ifndef PRODUCT_H #define PRODUCT_H #include <iostream> using std::istream; using std::ostream; using std::string; using std::endl; class Product { public: // constructor Product(unsigned barcode, double price, unsigned vat, const string& info): barcode_(barcode), price_(price), vat_(vat), info_(info) { } // Product initialiseren van stream Product(istream& is); // gegevens opvragen (accessor) const unsigned barcode() const { return barcode_; } const string& info() const { return info_; } double price() const { return price_; } unsigned vat() const { return vat_; } // gegevens wijzigen (modifier) void info(const string& info) { info_ = info; } void price(double price) { price_ = price; } void vat(unsigned vat) { vat_ = vat; } // lezen/schrijven van/naar streams bool operator==(Product& p); friend ostream& operator<<(ostream& os, const Product& product); private: unsigned barcode_; double price_; unsigned vat_; string info_; }; #endif
order.hCode:#include "product.h" Product::Product(istream& is) { unsigned barcode; double price; unsigned vat; string info; if(is >> barcode >> price >> vat) is.get(); getline(is,info); barcode_ = barcode; price_ = price; vat_ = vat; info_ = info; } ostream& operator<<(ostream& os, const Product& product) { os << product.barcode_ << " " << product.price_ << " " << product.vat_ << " " << product.info_ << endl; return os; }
order.ccCode:#ifndef ORDER_H #define ORDER_H #include <map> #include "product.h" using std::istream; using std::ostream; class Order { public: // Constructor Order() {} Order(istream& is); // Producten toevoegen en verwijderen unsigned addOrder(const Product& product, int quantity); unsigned cancelOrder(const Product& product, int quantity); // schrijven op stream friend ostream& operator<<(ostream& os, const Order& order); private: // Map p157 std::map <Product&, unsigned> products_; }; #endif
question #1Code:#include "order.h" Order::Order(istream& is) { while(is >> Product& >> quantity) { Product p(is); addOrder(p, quantity); cancelOrder (p, quantity) } } unsigned Order::addOrder(const Product& product, int quantity) { products[product]+=quantity; return products[product]; } unsigned Order::cancelOrder(const Product& product, int quantity) { if (products[product] <= quantity) { products.erase(product); return 0; } else { products[product]-=quantity; return products[product]; } }
Why do the first 3 files cmpile ok and the 4th(order.cc) doesn't
I just cant get order.cc bugfree
please help
question #2
How would i be able to store barcodes starting with 00 (preferably without using strings)
question #3
Would it be easy to stock all products in a multi-dimensional array, knowing that the description is a string ( in other words can i just ignore the fact that the description is a string and let the format conversion char -> int do it's work, ifso how)
thanks for your time and please ignore my ignorance
Jeffrey
sorry for the double post
You cannot have a map of references to Product. A reference is not copyable so it won't work with any of the stl containers.Code:std::map <Product&, unsigned> products_;
use
btw: are you shure you want Product to be the key of the map ? If so you need to define operator < for Product.Code:std::map <Product, unsigned> products_;
Kurt