Encounter problem with my assignment about class

Here's my program below. It's a very basic program with class and separate header file and stuff. I don't know where I made a mistake but the program won't run.

Please tell me where I got wrong. Much appreciated!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//this is my source.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include "InventoryItem.h"

using namespace std;
// This program is used to test class InventoryItem
int main()
{
	/*** Declare an InventoryItem object */
	/*** write code to test the mutator and accessor members  */
	//InventoryItem myItem;
	InventoryItem myItem("Default", 36);
	cout << myItem.getQuantityOnHand() << endl << myItem.getDescription() << endl;

	system("pause");
	return 0;
}


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
//this is my InventoryItem.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include "InventoryItem.h"

using namespace std;
//**********************************************************
// From here to the line of  asterisks is the implementation of class 
// InventoryItem

InventoryItem::InventoryItem()
{

}

InventoryItem::InventoryItem(string s, int i) 
{ 
	description = s;
	if (i > 0) quantityOnHand = i;
	else cout << "Invalid quantity entered!" << endl;
}


string InventoryItem::getDescription() 
/*** Add in-line implementation   */
{
	return description;
}


int InventoryItem::getQuantityOnHand()
/*** Add in-line implementation   */
{
	return quantityOnHand;
}

void InventoryItem::setDescription(string d)
// Post: description data member is set to string d; 
{
	/*** add code here */
	description = d;
}
void InventoryItem::setQuantityOnHand(int q)
// Post: set quantityOnHand to to q if q >= 0; unchanged otherwise;
{
	/*** add code here */
	if (q > 0) quantityOnHand = q;
	else cout << "Invalid Quantity" << endl;
}
//******************************************************* 


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
//this is my InventoryItem.h
// From here through the line of  asterisks is the specification of 
// class InventoryItem
#ifndef RECTANGLE_H 
#define RECTANGLE_H

using namespace std;

class InventoryItem
{
private:
	string description;  // The item description
	int quantityOnHand;          // Number of units on hand
public:
	/*** Class constructors need to be added  */
	InventoryItem();
	InventoryItem(string, int);


	// Accessor functions
	string getDescription();
	int getQuantityOnHand();
	// Mutator functions
	void setDescription(string);  // sets description member to argument
	void setQuantityOnHand(int);  // sets QuantityOnHand to argument
};
#endif 
Last edited on
You have a stray "vertical tab" character (0x0B) at the end of the #ifndef line in InventoryItem.h.
If you can't see it in your editor then delete that whole line and retype it.

You need to include <string> in InventoryItem.h. And you shouldn't say "using namespace std" in a header file. Just put std::string instead of string.

Fixing those allows it to compile but since I have no idea what it's supposed to do, I have no idea if it is working correctly.
Last edited on
Thank you so much for the reply tpb! I made changes to the places you pointed out and the program does compile successfully!

This program is just mainly used to test if my class works: if the constructors are correct, is the file separation correct, etc. Since that's the chapter we are on right.
So using namespace std should only be in the cpp files? And do I need to include all the library in all the files?
Last edited on
You should avoid putting "using namespace std" in header files because that kind of forces any other file that includes it to use the same directive. It should be up to each individual cpp file whether or not to use "using namespace std". It's generally considered to be a bad idea since it "dumps" all the identifiers (hundreds of them) into the global namespace where they can easily conflict with your own identifiers or those from other libraries (if they are dumped also). The point of namespaces is to keep all that stuff apart.

Only include the header files that are needed in each particular file. If a file says "std::vector" somewhere, then you need to include <vector>, etc. But try not to include headers that aren't needed in that specific file. Often headers that are required in the cpp file are not needed in the header file.
Last edited on
also why do you have:
2 private variables
4 public functions which set and get the variables doing nothing else? It's pointless.

get/set make sense either when you can do only one of them (get and not set or the opposite), or when they do something more (for example a setter could modify some other variable).

A stupid example, if you had 2 variables because for some reason you wanted to store the length separately (i know it doesnt make sense, it's just an example)
1
2
3
private: 
string description
int descriptionLength


Then it would make sense to have this:
1
2
3
4
5
6
public:
setDescription(string d)
    {
    description = d;
    descriptionLength = d.length();
    }


But since you have exactly one variable, and your getter and setter do nothing more nor less than getting and setting it without any restriction whatsoever, there's no point in not just making that variable public.
Topic archived. No new replies allowed.