C++ Object - Private int Returns Strange Values
I've been teaching myself C++ and started creating a list organizer to work on the concept of pointers.
I've defined a class called List which has three properties:
int itemTotal;
Item* firstItem;
Item* lastItem;
The constructor sets their values as:
itemTotal = 0;
firstItem = NULL;
lastItem = NULL;
I've built a function to return the value of itemTotal:
int List::getItemTotal()
{
return itemTotal;
}
Immediately after building the object in my driver, itemTotal starts acting funny and returning really large numbers (-858993460 every time), even though no work has been done on the List and literally nothing has happened in the program. I added a cout to the constructor, just to see what was going on there, and it echos out the 0 value, but once that constructor is done the value immediately changes.
I've been trying to work it out with my books and play around with it, but I can't seem to resolve the issue, so I figured I'd turn to someone with more experience. Main is below:
int main()
{
List grocery;
cout << "itemTotal is now: " << grocery.getItemTotal() << endl; // Returns wrong value...
system("Pause");
return 0;
}
With output looking like:
grocery List is built!
itemTotal inside of the constructor is 0!
itemTotal is now: -858993460
Any ideas? =/
EDIT: Per request, the entire class (sorry formatting is ugly, I don't want to re-do it all):
class List
{
public:
// Constructor
// Purpose: Builds object.
// Returns: Nothing.
// Pre-Conditions: None.
// Post-Conditions: Initializes null.
List();
// push_back function
// Purpose: Adds Item to end of List.
// Returns: None.
// Pre-Conditions: Must pass a declared Item object.
// Post-Conditions: None.
void push_back(Node*);
// push_front function
// Purpose: Adds Item to beginning of List.
// Returns: None.
// Pre-Conditions: Must pass a declared Item object.
// Post-Conditions: None.
void push_front(Node*);
// pop_back function
// Purpose: Removes last Item from List. Item is NOT deleted.
// Returns: Pointer to removed Item.
// Pre-Conditions: None.
// Post-Conditions: None.
Node* pop_back();
// pop_front function
// Purpose: Removes first Item from List. Item is NOT deleted.
// Returns: Pointer to removed Item.
// Pre-Conditions: None.
// Post-Conditions: None.
Node* pop_front();
// getFirst function
// Purpose: Returns pointer to first Item in List.
// Returns: Pointer.
// Pre-Conditions: List must have a Item object.
// Post-Conditions: None.
Node* getFirst();
// getItemTotal function
// Purpose: Returns the itemTotal
// Returns: Int
// Pre-Conditions: None.
// Post-Conditions: None.
int getItemTotal();
private:
Item* firstitem;
Item* lastitem;
int itemTotal;
}
and the constructor:
List::List(开发者_运维问答)
{
Item* firstNode = NULL;
Item* lastNode = NULL;
int itemTotal = 0;
cout << "item total should start at 0, it is " << nodeTotal << " inside of the constructor." << endl;
}
Haaa! You are initializing a local variable in the constructor not the member!! Instead of int itemTotal = 0;
write this->itemTotal = 0;
or just itemTotal = 0
or even use constructor initialization list like this
List::list()
:itemTotal(0),
firstNode(NULL),
lastNode(NULL)
{
cout << "List ctor Called"
}
You declared local values in your constructor with the same name as the members. They hide the value of the members, so when you set itemTotal = 0;
, you are actually just setting the value of a local variable. Your constructor should just look like this:
List::List()
:itemTotal(0), firstNode(NULL), lastNode(NULL)
{
cout << "item total should start at 0, it is " << itemTotal << " inside of the constructor." << endl;
}
You are initializing local variables and not class members.
Replace:
List::List()
{
Item* firstNode = NULL;
Item* lastNode = NULL;
int itemTotal = 0;
cout << "item total should start at 0, it is " << nodeTotal << " inside of the constructor." << endl;
}
By:
List::List()
{
firstNode = NULL;
lastNode = NULL;
itemTotal = 0;
cout << "item total should start at 0, it is " << nodeTotal << " inside of the constructor." << endl;
}
Your problem is that you are declaring local variables inside the constructor that hide the member variables:
List::List()
{
Item* firstNode = NULL; // declares a new variable.
Item* lastNode = NULL;
int itemTotal = 0;
cout << "item total should start at 0, it is " << nodeTotal << " inside of the constructor." << endl;
}
Your code should look like this:
List::List()
{
firstNode = NULL;
lastNode = NULL;
itemTotal = 0;
// fix this line: cout << "item total should start at 0, it is " << nodeTotal << " inside of the constructor." << endl;
}
It would be even better if you used an initializaer list:
List::List()
: firstNode(NULL)
, lastNode(NULL)
, itemTotal(0)
{
// Fix this line cout << "item total should start at 0, it is " << nodeTotal << " inside of the constructor." << endl;
}
精彩评论