The Compiler will not allow me to call a public method in another Class

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;
}
*/
You have to have a Project1::DeliveryChute object to call the insertProduct function.
Peter,

Do I need to make a DeliveryChute Object, after all in ProductRack.h there is a #include "deliveryChute.h", so all deliveryCHute public methods are also public from ProductRack.

And I have tried accessing one of DeliveryChute public method deliveryChute.retrieveProduct(), the one with no parameters; and the compiler accepted it.

It is when I access method that has a parameter that is declared private that I am having problem like this one.
DeliveryChute::insertProduct(Product *pProduct)

As soon as I add pProduct, the compiler complain that it is "private and inaccesible" it is even shown with a lock icon.

Do you have any clue how to get around my problem.
Do I need to make a DeliveryChute Object, after all in ProductRack.h there is a #include "deliveryChute.h", so all deliveryCHute public methods are also public from ProductRack.

Non-static class methods operate on class objects. If you do not have an object of that class with which to invoke the method, you cannot use the method.


And I have tried accessing one of DeliveryChute public method deliveryChute.retrieveProduct(), the one with no parameters; and the compiler accepted it.

Here you have an object of type DeliveryChute named deliveryChute. The difference between this and the other use? You have an object of the correct type to invoke the member method.

1
2
3
4
5
6
7
bool
Project1::ProductRack::addProduct(Product* pProduct)
{
    // Todo : Implementing 
    if (deliveryChute.insertProduct(pProduct) == false)
    return true;
}




Cire,

First of all thank your for responding, I am always hoping you and Vlad from Moscow will respond because it is always from the two of you who I get the most logical answer.

I agree with you, in this example deliveryChute is an object
1
2
3
4
5
6
7
bool
Project1::ProductRack::addProduct(Product* pProduct)
{
    // Todo : Implementing 
    if (deliveryChute.insertProduct(pProduct) == false)
    return true;
}


But I am not having an issue here. In the above function Project1::ProductRack::addProduct(Product* pProduct) it has
has a parameter Product* pProduct , it is very convinient because inside this function I have to call a function belonging to the deliveryChute class
deliveryChute.insertProduct(pProduct) which just so happen to need the same parameter as the function calling it. So it is just a trickle down.


But that is not where I am having a problem. My problem is when I call a Project1::ProductRack::deliverProduct() which has no parameter, to this function pProduct does not exist. But inside this function I have to call deliveryChute.insertProduct(pProduct) which needs pProduct. The only class that has pProduct is the private section of of the DeliveryChute class.
and if I enter it as a parameter of InserProduct, the compiler is telling me "it is private and inaccesible.

so how will I put pProduct as a parameter of insertProduct. This is a homework and the professor ties our hand a little bit. No friend function that will allow you access to the private section of the class.
I think you're trying to implement a design you don't understand.

My assumptions would be that ProductRack::addProduct was meant to add a product to it's store of products, not drop the proffered product down ye olde delivery chute.

I would expect deliverProduct to drop the product stored in products down the delivery chute.

Were you not given any sort of documentation describing how these classes were meant to work together?
Topic archived. No new replies allowed.