Error with Dynamic stack class

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 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  


COde for inventory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#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!
You are trying to create an InventoryItem like this
 
InventoryItem item;

but you only have this constructor
1
2
3
class InventoryItem{
InventoryItem(long, string);
}

So you must provide at least a number and a string or define a default constructor.
1
2
3
class InventoryItem{
InventoryItem();
}
Last edited on
how would I define a default constructor?
http://www.cplusplus.com/doc/tutorial/classes/

See: Constructors and Destructors
Topic archived. No new replies allowed.