Need help storing pointers into an array

I have a homework assignment where i have to use the new operator to store the pointers of InventoryItem objects in an array. Im not sure if im going about doing it right.

line 77-78 i created the array using new
in lines 91-92 im trying to store the pointers into the array but i get this error: Assigning to 'int' from incompatible type 'InventoryItem *'

Am i doing this correctly?

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  #include <string>
using namespace std;

class InventoryItem
{
private:
    string description; //item description
    double cost;        //Cost of item
    int units;          //Number of units on hand
public:
    //First constructor
    InventoryItem()
    {
        //initalize description, cost, and units
        description = "";
        cost = 0.0;
        units = 0;
    }
    //Second constructor
    InventoryItem(string desc)
    {
        //assign value to description
        description = desc;
        
        //initalize cost and units
        cost = 0.0;
        units = 0;
    }
    //Third constructor
    InventoryItem(string desc, double c, int u)
    {
        //assign value to description, cost, and units
        description = desc;
        cost = c;
        units = u;
    }
    
    //mutator function
    void setDescription(string d)
    {
        description = d;
    }
    void setCost(double c)
    {
        cost = c;
    }
    void setUnits(int u)
    {
        units = u;
    }
    
    //Accessor function
    string getDescription()const
    {
        return description;
    }
    double getCost()const
    {
        return cost;
    }
    int getUnits()const
    {
        return units;
    }
};




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

int main()
{
    const int NUM_ITEMS = 5;
    int *inventoryPtr = nullptr;
    inventoryPtr = new int[NUM_ITEMS];
    
    
    //inventoryItemArray = new InventoryItem*[NUM_ITEMS];
    InventoryItem inventory[NUM_ITEMS] =
    {
        InventoryItem("HAMMER", 6.95, 12),
        InventoryItem("WRENCH", 8.75, 20),
        InventoryItem("PLIERS", 3.75, 10),
        InventoryItem("RATCHET", 7.95, 14),
        InventoryItem("SCREWDRIVER", 2.50, 22)
    };
    
    for (int count = 0; count < NUM_ITEMS; count++)
        inventoryPtr[count] = &inventory[count];
    
    
    cout<< setw(14) <<"Inventory Item"
        << setw(8) << "Cost" <<setw(8)
        <<setw(16) << "Units on Hand\n";
    cout<< "-------------------------------------------------\n";
    
    for(int i = 0; i < NUM_ITEMS; i++)
    {
        cout<< setw(14) << inventory[i].getDescription();
        cout<<setw(8) << inventory[i].getCost();
        cout<< setw(7) << inventory[i].getUnits()<<endl;
    }
    
    return 0;
}








Last edited on
The error is telling you what is wrong.

inventoryPtr[count] = &inventory[count];

inventoryPtr is of type int pointer.
inventory is of time InventoryItem.

You can't store something of type InventoryItem in an array of int pointers.
I changed it to

1
2
InventoryItem *inventoryPtr = nullptr;
    inventoryPtr = new InventoryItem[NUM_ITEMS];


but now im getting a different error: No viable overloaded '='.
Last edited on
.
Last edited on
This is what i am at now
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <string>
using namespace std;

class InventoryItem
{
private:
    string description; //item description
    double cost;        //Cost of item
    int units;          //Number of units on hand
public:
    //First constructor
    InventoryItem()
    {
        //initalize description, cost, and units
        description = "";
        cost = 0.0;
        units = 0;
    }
    //Second constructor
    InventoryItem(string desc)
    {
        //assign value to description
        description = desc;
        
        //initalize cost and units
        cost = 0.0;
        units = 0;
    }
    //Third constructor
    InventoryItem(string desc, double c, int u)
    {
        //assign value to description, cost, and units
        description = desc;
        cost = c;
        units = u;
    }
    
    //mutator function
    void setDescription(string d)
    {
        description = d;
    }
    void setCost(double c)
    {
        cost = c;
    }
    void setUnits(int u)
    {
        units = u;
    }
    
    //Accessor function
    string getDescription()const
    {
        return description;
    }
    double getCost()const
    {
        return cost;
    }
    int getUnits()const
    {
        return units;
    }
};




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

int main()
{
    const int NUM_ITEMS = 5;
    InventoryItem *inventoryPtr = nullptr;
    inventoryPtr = new InventoryItem[NUM_ITEMS];
    
    
    //inventoryItemArray = new InventoryItem*[NUM_ITEMS];
    InventoryItem inventory[NUM_ITEMS] =
    {
        InventoryItem("HAMMER", 6.95, 12),
        InventoryItem("WRENCH", 8.75, 20),
        InventoryItem("PLIERS", 3.75, 10),
        InventoryItem("RATCHET", 7.95, 14),
        InventoryItem("SCREWDRIVER", 2.50, 22)
    };
    
    for (int count = 0; count < NUM_ITEMS; count++)
        inventoryPtr[count] = &inventory [count];
    cout<< &inventoryPtr[2]<<endl;
    
    
    cout<< setw(14) <<"Inventory Item"
        << setw(8) << "Cost" <<setw(8)
        <<setw(16) << "Units on Hand\n";
    cout<< "-------------------------------------------------\n";
    
    for(int i = 0; i < NUM_ITEMS; i++)
    {
        cout<< setw(14) << inventory[i].getDescription();
        cout<<setw(8) << inventory[i].getCost();
        cout<< setw(7) << inventory[i].getUnits()<<endl;
    }
    
    return 0;
}


On line 92 the "&" is giving me an error. its saying no viable overloaded "="
Last edited on
Topic archived. No new replies allowed.