Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compiler error: is private within this context

Tags:

c++

private

class

I'm writing a class and when I compile, I get one error message that says, "is private within this context" and another that says, "invalid use of non-static data member". But if I comment out everything before the addShipment function in my cpp file, it compiles just fine. Can someone please explain to me what is different to sometimes cause an error and sometimes not, and if the two different types of errors are related.

Product.h:

#ifndef PRODUCT_H
#define PRODUCT_H

#include <iostream>

class Product {
    public:
        Product(int productID, std::string productName);
        std::string getDescription();
        void setDescription(std::string description);
        std::string getName();
        int getID();
        int getNumberSold();
        double getTotalPaid();
        int getInventoryCount();
        void addShipment(int shipmentQuantity, double shipmentCost);
        void reduceInventory(int purchaseQuantity);
        double getPrice();
    private:
        int productID;
        std::string name;
        std::string description;
        double totalPaid;
        int inventoryCount;
        int numSold;
};

#endif

Product.cpp:

#include <iostream>
#include "Product.h"
using namespace std;

Product::Product(int productID, string productName) {
    Product::productID = productID;
    Product::name = productName;
}

string Product::getDescription() {
    return Product::description;
}

void Product::setDescription(string description) {
    Product::description = description;
}

string Product::getName() {
    return Product::name;
}

int Product::getID() {
    return Product::productID;
}

int Product::getNumberSold() {
    return Product::numSold;
}

double Product::getTotalPaid() {
    if(Product::totalPaid < 1) {
        totalPaid = 0;
    }
    return Product::totalPaid;
}

int Product::getInventoryCount() {
    Product::inventoryCount = 0; 
    return Product::inventoryCount;
}

void addShipment(int shipmentQuantity, double shipmentCost) {
    Product::inventoryCount = Product::inventoryCount + shipmentQuantity;
    Product::totalPaid = Product::totalPaid + shipmentCost;
}

void reduceInventory(int purchaseQuantity) {
    Product::inventoryCount = Product::inventoryCount - purchaseQuantity;
    Product::numSold = Product::numSold + purchaseQuantity;
}

double getPrice() {
    int price = (Product::totalPaid / static_cast<double>(Product::inventoryCount + Product::numSold)) * 1.25;
    return price;
}
like image 541
AdamK Avatar asked Apr 01 '17 07:04

AdamK


People also ask

Is private Within this context meaning?

The "private within this context" error refers to the fact that the functions addShipment , reduceInventory and getPrice are not members or friends of the class Product , so they cannot use its private members.

What is internal error in compiler?

An internal compiler error (ICE) results when the compiler can't process a source code file. When an ICE occurs, the compiler doesn't produce an output file, or any useful diagnostic that you can use to fix your code.


2 Answers

There are mainly 2 problems in your code, first, you are confusing static and non-static member variables and second, your last 3 functions are declared in the class declaration but you didn't qualify them with Product:: so they are free standing functions with the same names. If you intended them to be the definitions of the members, qualify them.

To fix the first point, when you have a class like the following:

struct some_class
{
    int nonstatic_member;
    static int static_member;
}

You access the static ones using the TypeName::MemberName syntax:

some_class::static_member = 5;

However, when a member is not static, you need an instance of that class to access the member:

some_class some_object;
some_object.nonstatic_member = 10;

The same rules apply for member functions as well:

void some_class::some_function()
{
    some_class::nonstatic_member = 10; // error
    this->nonstatic_member = 10; // fine
    nonstatic_member = 10; // fine

    static_member = 5; // fine
    some_class::static_member = 5; // fine
}

Since none of your member variables are static, your use of Product:: everywhere is the cause of the second error.

If you're not sure about the static member concept, here is a read: http://en.cppreference.com/w/cpp/language/static

like image 174
Fatih BAKIR Avatar answered Oct 28 '22 18:10

Fatih BAKIR


The "private within this context" error refers to the fact that the functions addShipment, reduceInventory and getPrice are not members or friends of the class Product, so they cannot use its private members.

The "invalid use of non-static data member" error refers to the fact that you are trying to access non-static data members by qualifying them using the syntax Class::member, which is how you access static data members, which are shared by all instances of a class. Non-static data members are accessed using the syntax object.member or pointer->member, as separate instances of each data member exist for each instance of a class.

And I assume that you mean the errors disappear if you comment out everything after (and including) the addShipment function, rather than everything before it. This is because both kinds of error refer to code within the non-member functions.

like image 26
Joseph Thomson Avatar answered Oct 28 '22 19:10

Joseph Thomson