Home:ALL Converter>compiler error: is private within this context

compiler error: is private within this context

Ask Time:2017-04-01T15:39:31         Author:AdamK

Json Formatter

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;
}

Author:AdamK,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43154155/compiler-error-is-private-within-this-context
Fatih BAKIR :

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.\n\nTo fix the first point, when you have a class like the following:\n\nstruct some_class\n{\n int nonstatic_member;\n static int static_member;\n}\n\n\nYou access the static ones using the TypeName::MemberName syntax:\n\nsome_class::static_member = 5;\n\n\nHowever, when a member is not static, you need an instance of that class to access the member:\n\nsome_class some_object;\nsome_object.nonstatic_member = 10;\n\n\nThe same rules apply for member functions as well:\n\nvoid some_class::some_function()\n{\n some_class::nonstatic_member = 10; // error\n this->nonstatic_member = 10; // fine\n nonstatic_member = 10; // fine\n\n static_member = 5; // fine\n some_class::static_member = 5; // fine\n}\n\n\nSince none of your member variables are static, your use of Product:: everywhere is the cause of the second error.\n\nIf you're not sure about the static member concept, here is a read: http://en.cppreference.com/w/cpp/language/static",
2017-04-01T12:40:18
yy