I am writing code for homework. It is an inventory program using a stack class and a inventory class. When I complile I get the following error:

Below is my code thus far.

Error   2   error C2512: 'InventoryItem' : no appropriate default constructor available c:\inventory.cpp    17  chapter18

Here is my code:

DynStack.h:

// Dynamic stack class that can hold objects of any class. 

// DynStack template

template <class T>
class DynStack
{
    private:
        struct StackNode
        {
            T value;
            StackNode *next;
        };

        StackNode *top;

    public:
        DynStack()
            {   top = NULL; }
        void push(T);
        void pop(T &);
        bool isEmpty();
};


//*********************************************************
// Member function push pushes the argument onto          *
// the stack.                                             *
//*********************************************************
template <class T>
void DynStack<T>::push(T num)
{
    StackNode *newNode;     //pointer to new node

    //Allocate a new node and store num there
    newNode = new StackNode;
    newNode->value = num;

    //If there are no nodes in the list, make the newNode first node.
    if (isEmpty())
    {
        top = newNode;
        newNode->next = NULL;
    }
    else    //otherwise, insert newNode before top
    {
        newNode->next = top;
        top =newNode;
    }
}

//*********************************************************
// Member function pop pops the value at the top          *
// of the stack off, and copies it into the variable      *
// passed as an argument.                                 *
//*********************************************************

template <class T>
void DynStack<T>::pop(T &num)
{
    StackNode *temp;    //temporary point

    //make sure the stack isn't empty
    if (isEmpty())
    {
        cout << "The stack is empty.\n";
    }
    else    //pop value off top of stack
    {
        num = top->value;
        temp = top->next;
        delete top;
        top = temp;
    }
}


//*********************************************************
// Member funciton isEmpty returns true if the stack      *
// is empty, or false otherwise.                          *
//*********************************************************

template <class T>
bool DynStack<T>::isEmpty()
{
    bool status;

    if (!top)
        status = true;
    else
        status = false; 

    return status;
}

Here is my code for InvItem.h:

// Specification file for the InvItem class

#ifndef INVITEM_H
#define INVITEM_H
#include <string>
using namespace std;

class InventoryItem
{
private:

    long serialNum;          //integer variable for serial number 
    string manufactDate;    //member that holds the date for part manfactured
    int lotNum;             //integer hold the lot number



public:
     // Constructor
    InventoryItem(long serialNum, string manufactDate);

    // define mutators here 
    void setSerialNum(long serial)
    { serialNum = serial; }

    void setManufactDate (string mDate)
    { manufactDate = mDate; }

    // define accessors here 
    long getSerialNum() const
    { return serialNum; }

    string getManufactDate() 
    { return manufactDate; }

};

#endif

And inventory.cpp:

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

int main()
{
    DynStack<InventoryItem> stack;   // create stack
    InventoryItem item;             // create inventory item object

    int choice;                     // Menu choice
    long serial;                    // Serial number
    string mDate;                   // Manufacture date

    do
    {
        // Display the menu.
        cout << "\n------ Inventory Menu --------\n\n";
        cout << "1.  Enter a part into the inventory.\n";
        cout << "2.  Take a part from the inventory.\n";
        cout << "3.  Quit.\n\n";
        cout << "Please make a choice (1, 2, or 3):  ";
        cin >> choice;

        // Validate choice
        while (choice < 1 || choice > 3)
        {
            cout << "Please enter 1, 2, or 3: ";
            cin >> choice;
        }

        // Act on the user's choice.
        switch(choice)
        {

        case 1:
            // Enter a part into inventory.
            cout << "\nYou have chosen to add an item to the inventory bin.\n\n";
            cout << "Enter the item's serial number: ";
            cin >> serial;
            item.setSerialNum(serial);
            cout << "Enter the item's manufacture date: ";
            cin >> mDate;
            item.setManufactDate(mDate);
            stack.push(item);
            break;

        case 2:
            // Take a part out of inventory.
            cout << "\nYou have chosen to remove an item from the inventory bin.\n\n";
            if (stack.isEmpty())
                cout << "No parts to remove.\n";
            else
            {
                stack.pop(item);
                cout << "\nThe part you removed was:\n";
                cout << "\tSerial number: " << item.getSerialNum() << endl;
                cout << "\tManufacture date: " << item.getManufactDate() << endl;
                cout << endl;
            }
            break;

        case 3:
            while (!stack.isEmpty())
            {
                stack.pop(item);
                cout << "\nThe part was left in inventory:\n";
                cout << "\tSerial number: " << item.getSerialNum() << endl;
                cout << "\tManufacture date: " << item.getManufactDate() << endl;
                cout << endl;
            }

            cout << "Goodbye!\n";
            break;
        }
    } while (choice  != 3);

    return 0;
}

ANY help would be great. I know it has something to do with my default constructor I aqm just not sure how to fix it. Any guidance is greatly appreciated!

Recommended Answers

All 3 Replies

I know it has something to do with my default constructor

You're correct. That "something" is the fact that you don't have a default constructor. A default constructor is only generated for you if there are no other user-defined constructors in the class.

so how do I resolve this error?

Add a default constructor?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.