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!!
//this is my source.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include "InventoryItem.h"
usingnamespace 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;
}
//this is my InventoryItem.h
// From here through the line of asterisks is the specification of
// class InventoryItem
#ifndef RECTANGLE_H
#define RECTANGLE_H
usingnamespace 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
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.
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 usingnamespace std should only be in the cpp files? And do I need to include all the library in all the files?
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.
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)
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.