-1

I am trying to write a program that manages a stores backend (i.e. keeping track of customers and products). I just started learning c++ in January and I am definitely a beginner with regards to classes and separate header files and such. I am getting a few undeclared identifier errors and a couple of unknown type name errors.

I am very aware that the code is rough, I am just trying to tackle this and get a good grip on how these interactions occur.

The main.cpp is MIA because it is hidden by a testing function created by my instructor.

The errors being received are as follows: https://i.stack.imgur.com/qymeo.jpg The locations of these errors are marked in the code by comments in order. Thank you so much in advance for any help, just want to get better at this.

Store.h

#ifndef Store_h
#define Store_h
#pragma once

#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>

class Store{

    std::vector<Product*> products;              //Error 1-2
    std::vector<Customer*> customers;            //Error 3-4

public:
    Store();
    Store(std::string name);
    std::string getStorename();
    void setStorename(std::string name);
    void addProduct(int productNum, std::string productName);
    Customer* getCustomer(int customerID);          //Error 5
    void addCustomer(int customerID, std::string name, bool credit);
    Product* getProduct(int productNum);           //Error 6
    void takeShipment(int productNum, int quantity, double cost);
    void makePurchase(int customerID, int productNum, int quantity);
    void takePayment(int customerID, double amount);
    void listProducts();
    void listCustomers();





private:
    std::string name;
};


#endif /* Store_h */

Store.cpp

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

Store::Store(){
}

Store::Store(string name): name(name) {

}


void Store::setStorename(string name) {
    this->name = name;
}

string Store::getStorename(){
    return name;

 }

void Store::addProduct(int productNum, string productName){

    Product* prod = new Product(productNum, productName);
    products.push_back(prod);


}

Product* Store::getProduct(int productNum){

    for (int i = 0; i < products.size(); ++i) {
        Product* a = products.at(i);
        if (a->getProductNum() == productNum) {
            return a;
        }
    }
    throw runtime_error("Invalid Product.");
}


void Store::addCustomer(int customerID, string name, bool credit){

    Customer* cust = new Customer(name, customerID, credit);
    customers.push_back(cust);


}

Customer* Store::getCustomer(int customerID){

    for (int i = 0; i < customers.size(); ++i) {
        Customer* a = customers.at(i);
        if (a->getID() == customerID) {
            return a;
        }
    }
    throw runtime_error("Invalid Customer.");

}

void Store::takeShipment(int productNum, int quantity, double cost){

    for (int i = 0; i < products.size(); ++i) {
        Product* a = products.at(i);
        if (a->getProductNum() == productNum) {



    }
}
throw runtime_error("Invalid Product.");


}

void Store::makePurchase(int customerID, int productNum, int quantity){

    Customer* a = getCustomer(customerID);
    a->processPurchase(productNum, product); //ERROR 'use of undeclared        
                                             //  variable 'product'


}

void Store::takePayment(int customerID, double amount){

    Customer* a = getCustomer(customerID);
    a->processPayment(amount);


}

void Store::listProducts(){
    for (int i = 0; i < products.size(); ++i) {
        Product* prod = products.at(i);
        prod->getInfo();
        cout << endl;
    }
}

void Store::listCustomers(){
    for (int i = 0; i < customers.size(); ++i) {
        Customer* prod = customers.at(i);
        prod->getInfo();
        cout << endl;
    }


}

Product.h

#ifndef Product_h
#define Product_h
#pragma once


#include <stdio.h>
#include "Customer.h"
#include "Store.h"
#include <string>
#include <iostream>
#include <vector>

class Product{

public:
    Product();
    Product(int productNum, std::string productName);
    void setDescription(std::string description);
    std::string getDescription();
    void setDefaultReturnPeriod(int days);
    int getReturnPeriod();
    int getNumberSold();
    double getTotalCost();
    void addShipment(int quantity, double cost);
    double getPrice();
    void processOrder(int quantity);
    int getProductNum();
    void getInfo();
protected:
    std::string productName;
private:

    int productNum;
    int inventory;
    int numSold;
    int totalCost;
    std::string description;
    int defaultReturnDays;
    int quantity;
    double cost;
};

#endif /* Product_h */

Customer.h

#ifndef Customer_h
#define Customer_h
#pragma once

#include <stdio.h>
#include <string>
#include <iostream>
#include "Store.h"
#include <vector>

class Customer{

public:
    Customer();
    Customer(std::string name, int customerID, bool credit);
    void setName(std::string name);
    std::string getName();
    int getID();
    void setCredit(bool hasCredit);
    bool getCredit();
    double getBalance();
    void processPayment(double amount);
    void processPurchase(double amount, Product product);  //Error 7
    void getInfo();


private:

    std::string memberName;
    std::string name;
    int customerID;
    bool credit;
    double balance;
    std::vector<Product*> productsPurchased;     //Error 8


};



#endif /* Customer_h */
  • In `store.h` [forward-declare](http://stackoverflow.com/questions/4757565/c-forward-declaration) `Product` and `Customer`. In `Customer.h` also include `Product.h` -- or perhaps better not take the `product` parameter by value in `processPurchase(...)`. – Dan Mašek Apr 01 '16 at 04:37
  • `Store.cpp` has no idea what a `Product` is once it starts compiling the preprocessor-slurped-in `Store.h` content, long before it encounters the likewise slurped-in `Product.h` content. It doesn't *need* to know due to the pointer usage rather than instance usage, but it at-least needs to know there *is such a thing*. You've given it no such info. Forward-decls would likely solve your issue. Btw, unrelated, I see no earthly reason why `Customer.h` has to pull in `Store.h` *at all*. Likewise for `Product.h`. – WhozCraig Apr 01 '16 at 04:38
  • You are using `Customer` and `Product` before they are initialized in the program. Try prototyping those classes. – Kilo King Apr 01 '16 at 04:38

1 Answers1

2

When the compiler processes the lines:

std::vector<Product*> products;              //Error 1-2
std::vector<Customer*> customers;            //Error 3-4

it does not know anything about Product or Customer. You can forward declare them to let the compiler know that they are classes.

Add the following lines before the definition of Store begins.

class Product;
class Customer;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I now have an error pertaining to the makePurchase function located in Store.cpp. The parameters of the processPurchase function are (double amount, Product& product), but when I enter those in the makePurchase function I receive the error stating "Use of undefined identifier 'product'. at this point thats the last error I have. I will edit the existing code to display the error. Thank you R Sahu and others for helping me realize such a simple mistake!! – Micheal Reyes Apr 01 '16 at 06:56
  • @MichealReyes, you don't have a variable named `product` in `Store::makePurchase`. You have a `productNum`. You'll need to add some code to get the product given the product number, and then use it in the calll to `Customer::processPurchase`. – R Sahu Apr 01 '16 at 12:59