I keep getting an error about m_pHead in printStoresInfo(); function in the cpp
ID: 3797889 • Letter: I
Question
I keep getting an error about m_pHead in printStoresInfo(); function in the cpp file.
CustomerList.h
#pragma once;
#include
#include "Store.h"
class CustomerList
{
public:
Store *m_pHead;
CustomerList();
~CustomerList();
bool addStore(Store *s);
Store *removeStore(int ID);
Store *getStore(int ID);
bool updateStore(int ID, char *name, char *addr, char *city, char *st, char *zip);
void printStoresInfo();
};
CustomerList.cpp
#include
#include "CustomerList.h"
#include "Store.h"
using namespace std;
CustomerList::CustomerList()
{
// Default constructor
}
CustomerList::~CustomerList()
{
// Destructor
}
bool CustomerList:: addStore(Store *s)
{
//creating a new instance
s = new Store();
if(s==NULL)
return true;
}
Store *CustomerList::removeStore(int ID)
{
Store *temp, *back;
temp = m_pHead;
back = NULL;
while((temp != NULL)&&(ID != temp ->getStoreID()))
{
back=temp;
temp=temp->m_pNext;
}
if(temp==NULL)
return NULL;
else if(back==NULL)
{
m_pHead = m_pHead ->m_pNext;
return temp;
}
else
{
back -> m_pNext = temp-> m_pNext;
return temp;
}
return NULL;
}
Store *CustomerList::getStore(int ID)
{
Store *temp;
temp = m_pHead;
while((temp != NULL) && (ID != temp ->getStoreID()))
{
temp = temp->m_pNext;
}
if(temp == NULL)
return NULL;
else
{
Store *retStore = new Store;
*retStore = *temp;
retStore->m_pNext = NULL;
return retStore;
}
}
bool CustomerList::updateStore(int ID, char *name, char *addr, char *city, char *st, char *zip)
{
return false;
}
void CustomerList::printStoresInfo()
{
Store *temp;
cout << " ===================================================================================== " << endl;
if(m_pHead == NULL)
{
cout << " The List is currently empty. " ;
}
else
{
temp = m_pHead;
while(temp != NULL)
{
cout << temp->m_pNext << endl;
}
}
}
Store.h
#pragma once;
#include <string.h>
#include <iostream>
using namespace std;
class Store
{
private:
int m_iStoreID;
char m_sStoreName[64];
char m_sAddress[64];
char m_sCity[32];
char m_sState[32];
char m_sZip[11];
public:
Store *m_pNext;
Store(); // Default constructor
Store(int ID, // Constructor
char *name,
char *addr,
char *city,
char *st,
char *zip);
~Store(); // Destructor
int getStoreID(); // Get/Set store ID
void setStoreID(int ID);
char *getStoreName(); // Get/Set store name
void setStoreName(char *name);
char *getStoreAddress(); // Get/Set store address
void setStoreAddress(char *addr);
char *getStoreCity(); // Get/Set store city
void setStoreCity(char *city);
char *getStoreState(); // Get/Set store state
void setStoreState(char *state);
char *getStoreZip(); // Get/Set store zip code
void setStoreZip(char *zip);
void printStoreInfo(); // Print all info on this store
};
Store.cpp
#include "Store.h"
#include <iomanip>
using namespace std;
Store::Store()
{
m_pNext = NULL;
}
Store::Store(int ID, char *name, char *addr, char *city, char *st, char *zip)
{
m_iStoreID = ID;
strcpy(m_sStoreName, name);
strcpy(m_sAddress, addr);
strcpy(m_sCity, city);
strcpy(m_sState, st);
strcpy(m_sZip, zip);
m_pNext = NULL;
}
Store::~Store()
{
// Nothing to do here
}
int Store::getStoreID()
{
return m_iStoreID;
}
void Store::setStoreID(int ID)
{
m_iStoreID = ID;
}
char *Store::getStoreName()
{
char *name = new char[strlen(m_sStoreName) + 1];
strcpy(name, m_sStoreName);
return name;
}
void Store::setStoreName(char *name)
{
strcpy(m_sStoreName, name);
}
char *Store::getStoreAddress()
{
char *addr = new char[strlen(m_sAddress) + 1];
strcpy(addr, m_sAddress);
return addr;
}
void Store::setStoreAddress(char *addr)
{
strcpy(m_sAddress, addr);
}
char *Store::getStoreCity()
{
char *city = new char[strlen(m_sCity) + 1];
strcpy(city, m_sCity);
return city;
}
void Store::setStoreCity(char *city)
{
strcpy(m_sCity, city);
}
char *Store::getStoreState()
{
char *state = new char[strlen(m_sState) + 1];
strcpy(state, m_sState);
return state;
}
void Store::setStoreState(char *state)
{
strcpy(m_sState, state);
}
char *Store::getStoreZip()
{
char *zip = new char[strlen(m_sZip) + 1];
strcpy(zip, m_sZip);
return zip;
}
void Store::setStoreZip(char *zip)
{
strcpy(m_sZip, zip);
}
void Store::printStoreInfo()
{
cout << m_iStoreID << setw(20) << m_sStoreName << setw(15) << m_sAddress
<< setw(15) << m_sCity << ", " << m_sState << setw(10) << m_sZip << " ";
}
Explanation / Answer
Made changes to the following files
PROGRAM CODE:
CustomerList.cpp
/*
* CustomerList.cpp
*
* Created on: 25-Feb-2017
* Author: kasturi
*/
#include
#include "CustomerList.h"
#include "Store.h"
using namespace std;
CustomerList::CustomerList()
{
m_pHead = NULL;
// Default constructor
}
CustomerList::~CustomerList()
{
delete m_pHead;
// Destructor
}
bool CustomerList:: addStore(Store *s)
{
//creating a new instance
if(m_pHead==NULL)
{
m_pHead = new Store(s->getStoreID(),s->getStoreName(), s->getStoreAddress(), s->getStoreCity(), s->getStoreState(), s->getStoreZip());
return true;
}
else
{
Store * temp;
temp=m_pHead;
while(temp->m_pNext != NULL)
{
temp = temp->m_pNext;
}
temp->m_pNext = new Store(s->getStoreID(),s->getStoreName(), s->getStoreAddress(), s->getStoreCity(), s->getStoreState(), s->getStoreZip());
return true;
}
}
Store *CustomerList::removeStore(int ID)
{
Store *temp, *back;
temp = m_pHead;
back = NULL;
while((temp != NULL)&&(ID != temp ->getStoreID()))
{
back=temp;
temp=temp->m_pNext;
}
if(temp==NULL)
return NULL;
else if(back==NULL)
{
m_pHead = m_pHead ->m_pNext;
return temp;
}
else
{
back -> m_pNext = temp-> m_pNext;
return temp;
}
return NULL;
}
Store *CustomerList::getStore(int ID)
{
Store *temp;
temp = m_pHead;
while((temp != NULL) && (ID != temp ->getStoreID()))
{
temp = temp->m_pNext;
}
if(temp == NULL)
return NULL;
else
{
Store *retStore = new Store;
*retStore = *temp;
retStore->m_pNext = NULL;
return retStore;
}
}
bool CustomerList::updateStore(int ID, char *name, char *addr, char *city, char *st, char *zip)
{
return false;
}
void CustomerList::printStoresInfo()
{
Store *temp;
cout << " ===================================================================================== " << endl;
if(m_pHead == NULL)
{
cout << " The List is currently empty. " ;
}
else
{
temp = m_pHead;
while(temp != NULL)
{
temp->printStoreInfo();
temp = temp->m_pNext;
}
}
}
Store.cpp
/*
* Store.cpp
*
* Created on: 25-Feb-2017
* Author: kasturi
*/
#include "Store.h"
#include <iomanip>
using namespace std;
Store::Store()
{
m_pNext = NULL;
}
Store::Store(int ID, char *name, char *addr, char *city, char *st, char *zip)
{
m_iStoreID = ID;
strcpy(m_sStoreName, name);
strcpy(m_sAddress, addr);
strcpy(m_sCity, city);
strcpy(m_sState, st);
strcpy(m_sZip, zip);
m_pNext = NULL;
}
Store::~Store()
{
// Nothing to do here
}
int Store::getStoreID()
{
return m_iStoreID;
}
void Store::setStoreID(int ID)
{
m_iStoreID = ID;
}
char *Store::getStoreName()
{
char *name = new char[strlen(m_sStoreName) + 1];
strcpy(name, m_sStoreName);
return name;
}
void Store::setStoreName(char *name)
{
strcpy(m_sStoreName, name);
}
char *Store::getStoreAddress()
{
char *addr = new char[strlen(m_sAddress) + 1];
strcpy(addr, m_sAddress);
return addr;
}
void Store::setStoreAddress(char *addr)
{
strcpy(m_sAddress, addr);
}
char *Store::getStoreCity()
{
char *city = new char[strlen(m_sCity) + 1];
strcpy(city, m_sCity);
return city;
}
void Store::setStoreCity(char *city)
{
strcpy(m_sCity, city);
}
char *Store::getStoreState()
{
char *state = new char[strlen(m_sState) + 1];
strcpy(state, m_sState);
return state;
}
void Store::setStoreState(char *state)
{
strcpy(m_sState, state);
}
char *Store::getStoreZip()
{
char *zip = new char[strlen(m_sZip) + 1];
strcpy(zip, m_sZip);
return zip;
}
void Store::setStoreZip(char *zip)
{
strcpy(m_sZip, zip);
}
void Store::printStoreInfo()
{
cout << m_iStoreID << setw(20) << m_sStoreName << setw(15) << m_sAddress
<< setw(15) << m_sCity << ", " << m_sState << setw(10) << m_sZip << " ";
}
//I used this main file for texting
simpleMain.cpp
/*
* simpleMain.cpp
*
* Created on: 25-Feb-2017
* Author: kasturi
*/
#include "Store.h"
#include "CustomerList.h"
#include <iostream>
int main()
{
CustomerList list;
list.printStoresInfo();
Store s1(123, "KS Stores", "Wells Street", "New York", "New York", "A50032");
Store s2(124, "Free Stores", "Wall Street", "New York", "New York", "A50032");
Store s3(125, "Real Stores", "KC Street", "Washington", "Washington D.C", "D50032");
list.addStore(&s1);
list.addStore(&s2);
list.addStore(&s3);
list.removeStore(124);
list.printStoresInfo();
return 0;
}
OUTPUT:
=====================================================================================
The List is currently empty.
=====================================================================================
123 KS Stores Wells Street New York, New York A50032
125 Real Stores KC Street Washington, Washington D.C D50032