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;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With