Hi to all,
I hope you are not getting tired of helping me. I have read the book over and over and I thought I understand "CLASS". But when I applied it and write the code the compiler tells me that there is a compiling error.
1. I have this method addProduct(Product* pProduct)
in a Class called ProductRack, the code is in ProductRack.cpp file. The declaration of the Class methods and private variables are in ProductRack.h header file.
2. But when I call a method in the DeliveryCHute Class from the ProductRack Class I get a compiler errors which are these:
A.IntelliSense: a nonstatic member reference must be relative to a specific object
B.error C2352: 'Project1::DeliveryChute::insertProduct' : illegal call of non-static member function
And this is causing the error:
if (Project1::DeliveryChute::insertProduct(pProduct) == false)
3. Can Somebody tell me what I am doing wrong? Like I said I have read the book over for about 5 times. So I am lost why I get the .errors
1 2 3 4 5 6 7 8
|
//THIS IS JUST ONE METHOD INSIDE ProductRack.cpp
bool
Project1::ProductRack::addProduct(Product* pProduct)
{
// Todo : Implementing
if (Project1::DeliveryChute::insertProduct(pProduct) == false)
return true;
}
|
NOTE: You can see that this header file includes "DeliveryChute.h"
so I have access to all its public method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
//THIS IS THE HEADER FILE ProductRack.h
#include "DeliveryChute.h"
#include "Product.h"
#include "StatusPanel.h"
namespace Project1
{
class ProductRack
{
public:
static const int MAX_PRODUCTS = 5;
ProductRack(
StatusPanel &statusPanel,
const char *allowedProductName,
DeliveryChute &deliveryChute,
unsigned productPriceCents);
~ProductRack();
bool isCompatibleProduct(const char *productName) const;
bool isEmpty() const;
bool isFull() const;
bool addProduct(Product* pProduct);
bool deliverProduct();
unsigned getNumProductsInRack() const;
unsigned getProductPriceCents() const;
private:
StatusPanel &statusPanel;
char allowedProductName[Product::MAX_NAME_LENGTH];
DeliveryChute &deliveryChute;
Product *products[MAX_PRODUCTS];
unsigned productCount;
unsigned productPriceCents;
};
}
|
NOTE: This Header file conatains the declaration of Class DeliveryChute and I am only interested in the public method bool insertProduct(Product *pProduct);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
//THIS IS THE HEADER FILE DeliveryChute.h
#include "Product.h"
#include "StatusPanel.h"
namespace Project1
{
class DeliveryChute
{
public:
DeliveryChute(StatusPanel &statusPanel);
~DeliveryChute();
bool insertProduct(Product *pProduct);
Product *retrieveProduct();
private:
bool containsProduct() const;
StatusPanel statusPanel;
Product *pProduct;
};
}
|
NOTE: This .cpp file contains all the code of methods declared in the header file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
//THIS IS IN DeliveryChute.cpp file
Project1::DeliveryChute::DeliveryChute(StatusPanel &statusPanel)
: statusPanel(statusPanel),
pProduct(0)
{
// done: Implementing
cout << " Delivery Chute Class Constructor called\n ";
//statusPanel.MESSAGECODE_SOLD_OUT;
statusPanel.displayMessage(Project1::StatusPanel::MESSAGECODE_SOLD_OUT);
}
Project1::DeliveryChute::~DeliveryChute()
{
// done: Implementing
cout << " Delivery Chute Class Destructor called\n ";
}
// If there is a product in the chute it return a message that the chute is occupied and return a false
// If the chute is empty the product is put in the chute
bool
Project1::DeliveryChute::insertProduct(Product *pProduct)
{
// done: Implementing
if (containsProduct())
{
statusPanel.displayMessage(Project1::StatusPanel::MESSAGECODE_CHUTE_FULL);
return false;
}
else
{
this->pProduct = pProduct;
return true;
}
}
Project1::Product *
Project1::DeliveryChute::retrieveProduct()
{
// done: Implementing m
return pProduct;
}
// Function checks the brand if it is not zero indicating there is something in the chute
// I could use if(pProduct->getName()) and the logic stays the same.
bool
Project1::DeliveryChute::containsProduct() const
{
// done: Implementing
if (pProduct->getBrand())
return true;
else
return false;
}
|
*/