0

I'm trying to calculate the total cost of inventory items using the inventory class, i'm getting a few error messages and i can't figure out why, the errors are undeclared identifiers in the imp file of my ... Here's what i have so far.

Inventory.h

#include <string>
#include <iostream>
using namespace std;


class Inventory 
{
public:
    void print() const;
    void setItemNumber(int num);
    void setQuantity(int qty);
    void setCost(double cst);
    void setTotalCost(double total);
    int getItemNumber() const;
    int getQuantity() const;
    double getCost() const;
    double getTotalCost () const;

    Inventory(int num = 0, int qty = 0, double cst = 0, double total = 0);
    Inventory(int num, int qty, double cst, double total);


private:
    int itemNumber;
    int quantity;
    double cost;
    double totalCost;


};

InventoryImp.cpp

#include <iostream>
#include "InventoryClass.h"

using namespace std;


void Inventory::print() const
{
    if (itemNumber > 0 && quantity > 0 && cost > 0)
        cout << itemNumber << quantity << cost
                << totalCost;
}

void Inventory::setCost(double cst)
{
    cost = cst;
}

void Inventory::setItemNumber(int num)
{
    itemNumber = num;
}

void Inventory::setQuantity(int qty)
{
    quantity = qty;
}

void Inventory::setTotalCost(double total)
{
    totalCost = total;
}

double Inventory::getCost() const
{
    return cst;
}

int Inventory::getItemNumber() const
{
    return num;
}

int Inventory::getQuantity() const
{
    return qty;
}

double Inventory::getTotalCost() const
{
    return qty * cst;
} 


Inventory::Inventory(int num, int qty, double cst, double total)
{
    cost = cst;
    quantity = qty;
    totalCost = total;
    itemNumber = num;
}
Inventory::Inventory(int num, int qty, double cst, double total)
{
    cost = cst;
    quantity = qty;
    totalCost = total;
    itemNumber = num;
}

Main.cpp

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

int main()
{
    Inventory Item1(101, 6, 3.00);
    Inventory Item2(102, 1, 1.00);
    Inventory Item3(103, 8, 7.00);
    Inventory Item4(104, 4, 12.00);
    Inventory Item5(105, 6, 5.00);
    Inventory Item6(106, 3, 9.00);

    Item1.print();
    cout << endl;
    Item2.print();
    cout << endl;
    Item3.print();
    cout << endl;
    Item4.print();
    cout << endl;
    Item5.print();
    cout << endl;
    Item6.print();
    cout << endl;

    return 0;
}
Shimar
  • 49
  • 2
  • 6
  • 2
    And you have done exactly what to debug the problem(s)? – Puppy Apr 27 '11 at 13:45
  • 4
    what errors? we cannot smell your errors or magically guess them, you need to explicitly post the messages – Tony The Lion Apr 27 '11 at 13:46
  • You seem to have forgotten to post the error messages. – forsvarir Apr 27 '11 at 13:46
  • 1) Fix problems as they arise, don't add to code that doesn't work, 2) try to fix problems yourself *before* you post here, and if you aren't willing to put in that much effort then 3) go away. – Beta Apr 27 '11 at 13:52
  • I've been trying to fix this problem on my own for the past three days on my own... i hate when people say that, i'm not lazy like other people, i'm currently still working on myself to see if i can fix before anyone says anything. The error messages i get are several undeclared identifiers . – Shimar Apr 27 '11 at 13:59
  • 1
    @Shimar, look at the first error only. Attempt to fix that first, then re-build and see what errors are left. Often a simple error can cause hundreds, if not thousands of other unrelated errors. Also, accept answers on the other questions, else no-one will want to help. – Mark Ingram Apr 27 '11 at 14:01
  • @Shimar: what *exact* errors (add them in to the question preferably) are you getting? How are you compiling the code? – forsvarir Apr 27 '11 at 14:07
  • @Mark Ingram thank you for your help, i didn't know i had to accept the answers for the other questions. – Shimar Apr 27 '11 at 14:10
  • 1
    Is the header file called "Inventory.h" or "InventoryClass.h"? And which identifiers are the error messages referring to? – Mike Seymour Apr 27 '11 at 14:28
  • 1
    Another thing to consider (although not related to your problem), is that your header file doesn't appear to have re-include guards. When you start having more headers, possibly including each other, this can become quite important. You might want to take a look at: http://stackoverflow.com/questions/1143936/pragma-once-vs-include-guards – forsvarir Apr 27 '11 at 14:34
  • @Mike Seymour, it refers to all the identifiers in my imp file in the get functions. everything else runs fine but those. – Shimar Apr 27 '11 at 14:43
  • @Shimar: "all the identifiers in my imp file in the get functions" - do you mean the names of the functions (`getCost` etc.) or the member variables referenced in the functions (`cst` etc.)? The question would be a lot easier to answer if you showed us the compiler errors; they often tell you exactly what the problem is, if you know how to read them. – Mike Seymour Apr 27 '11 at 14:52
  • @Shimar: actually, I've just noticed that your `get` functions are using the wrong names for the member variables (e.g. `cst` rather than `cost`). This is the cause of at least some of your errors. – Mike Seymour Apr 27 '11 at 14:54
  • @Mike Seymour: If you're interested, I believe we've got to the bottom of what the errors reported are in the comments for Ducks answer...apparently you noticed anyway :) – forsvarir Apr 27 '11 at 14:55
  • Shimar, I apologize, my criticism was too harsh. I know what it is to be a novice, but I did not imagine that you could have known enough to have written all that code, put in so much effort and still not seen the bugs, so I jumped to the conclusion that you had not tried. I urge you to consider my other point; if you had noticed each bug sooner, and stopped to fix it before adding more code, your job would have been much simpler and less frustrating. – Beta Apr 27 '11 at 19:05

2 Answers2

4

For a start, you only need one instance of this function...

Inventory(int num = 0, int qty = 0, double cst = 0, double total = 0);     

The second one, with the same parameters and no default values is redundant, the compiler isn't going to be able to differentiate between them.

forsvarir
  • 10,243
  • 6
  • 38
  • 70
3

Aside from the main problem that forsvarir addressed, your other errors seem to revolve around undefined variables. You actually define them but misspell them in a couple of places.

Start training yourself to use some convention (it really doesn't matter what so long as you are consistent) that differentiates between member variables, global variables, parameters, etc. For instance naming member variables "_member" or "mMember" or whatever. It will save yourself and others grief down the road.

Duck
  • 25,228
  • 3
  • 57
  • 86
  • Hah! I had a cursory glance and couldn't see any obvious mistakes... apparently I completely ignored all of the get methods. Good advice on adpoting a naming convention (personally I hate the remove half the letters approach) – forsvarir Apr 27 '11 at 14:24
  • 1
    @Shimar: all of your get methods on the inventory class are using the shortened versions of names, not the versions of names that the class knows about (it's member variables). So you return 'qty' rather than 'quantity' from 'getQuantity' for example... – forsvarir Apr 27 '11 at 14:42