Errors C2228 & C2065

Just started a programming class this semester, and haven't been having much trouble with it thus far. However, after writing around five classes without a problem earlier, this one has been giving me trouble. I keep getting errors C2228 and C2065 on lines 108-111. This is my first class that uses two constructors, so my guess is there's some sort of syntax error related to that. Any help would be greatly appreciated.

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
#include <iostream>
#include <iomanip>
using namespace std;

class Inventory
{
private:
	int itemNumber, quantity;
	double cost, tC;
	int validateInput(int);
	double validateInputCost(double);

public:
	Inventory();
	Inventory(int itemNumber, int quantity, double cost);
	void setItemNumber(int); 
	void setQuantity(int);	
	void setCost(double);	
	int getItemNumber();	
	int getQuantity();
	double getCost();
	double getTotalCost();
};
Inventory::Inventory()
{
	itemNumber = 0;
	quantity = 0;
	cost = 0;
}
Inventory::Inventory(int i, int q, double c)
{
	setItemNumber(i);
	setQuantity(q);
	setCost(c);
	tC = getTotalCost();
}
void Inventory::setItemNumber(int i)
{
	itemNumber = validateInput(i);
}
void Inventory::setQuantity(int q)
{
	quantity = validateInput(q);
}
void Inventory::setCost(double c)
{
	cost = validateInputCost(c);
}
int Inventory::getItemNumber()
{
	return itemNumber;
}
int Inventory::getQuantity()
{
	return quantity;
}
double Inventory::getCost()
{
	return cost;
}
double Inventory::getTotalCost()
{
	tC = cost * quantity;
	return tC;
}
int Inventory::validateInput(int a)
{
	if (a < 0)
	{
		cout << "Invalid input (negative number). Will set to default (0).\n";
		return 0;
	}
	else
		return a;
}
double Inventory::validateInputCost(double b)
{
	if (b < 0)
	{
		cout << "Invalid input (negative number). Will set to default (0).\n";
		return 0;
	}
	else
		return b;
}

int main()
{
	int choice, itemNumber, quantity;
	double cost, totalCost;
	cout << "Please choose an option:\n 1 for default values\n 2 for manual entry\n";
	cin >> choice;

	if (choice == 2)
	{
		cout << "Please enter the item's number:\n";
		cin >> itemNumber;
		cout << "Please enter the item's quantity:\n";
		cin >> quantity;
		cout << "Please enter the item's individual cost:\n";
		cin >> cost;
		Inventory item1 (itemNumber, quantity, cost);
	}
	else
		Inventory item1;

	itemNumber = item1.getItemNumber();
	quantity = item1.getQuantity();
	cost = item1.getCost();
	totalCost = item1.getTotalCost();
	cout << "Item number is: " << itemNumber << endl;
	cout << "Item quantity is: " << quantity << endl;
	cout << "Item cost is: $" << cost << endl;
	cout << "Total cost is: $" << totalCost << endl;
	return 0;
}

Last edited on
Inventory item1 (itemNumber, quantity, cost);
This is being defined inside of an if statement so it has local scope there and can not be used outside of that block of code (in between the braces). This applies for the object created in the else statement as well.

You may want to look up "Local/Global Variable Scope" on some C++ topics to understand why this is happening.

You could use dynamic memory/pointers to create the object in either of the blocks but in this case I would just define your starting variables equal to 0 and then use the same constructor after both choices.
Ah, thanks. Always a frustrating feeling when the solution is so simple.
Topic archived. No new replies allowed.