I've been filling in bits and pieces of this code, and the part I'm having difficulty with is the List::insert() function in List.cpp.
When I try to do anything with headByName or headByRating I get
"Unhandled exception at 0x00412f2c in lab1.exe: 0xC0000005: Access violation writing location 0x00000004."
What am I not setting up right? I'm not allowed to change the header files for this assignment so I have to focus on the cpp files. I've done something to break it.
Any help would be very appreciated.
List.cpp (this is the one I'm working on, the other files are to look at if you need more clarification)
// make NO CHANGES to this file
#pragma once // include this .h file file only once
#include <ostream>
#include "winery.h"
usingnamespace std;
class List
{
public:
List(void); // constructor
virtual ~List(void); // destructor
// Print out the wineries in alphabetical order by name,
// by calling winery's operator<< for each winery.
void displayByName(ostream& out) const;
// Print out the wineries from highest to lowest rating,
// by calling winery's operator<< for each winery.
void displayByRating(ostream& out) const;
// Insert a winery into both the names and ratings threads.
// The names thread should be in alphabetical order by name.
// The ratings thread should be in order from highest rating
// to lowest rating.
void insert(const Winery& winery);
// Return a const pointer to the winery instance it finds in
// the list, or 0 if it didn't find a winery with that name.
// Because the pointer is declared const, there is no danger
// that find's caller will be able to use the returned pointer
// to change the instance of winery.
Winery * const find(constchar * const name) const;
// Remove the winery with the specified name from both the name
// thread and the ratings thread. Returns true if it found and
// removed the winery, false if it did not find the winery.
bool remove(constchar * const name);
private:
// defines each node in the doubly-threaded linked list.
struct Node
{
Node(const Winery& winery); // constructor
Winery item; // an instance of winery
// (NOT a pointer to an instance)
Node *nextByName; // next node in the name thread
Node *nextByRating; // next node in the rating thread
};
Node *headByName; // first node in the name thread
Node *headByRating; // first node in the rating thread
};
// make NO CHANGES to this file
#pragma once // include this .h file file only once
#include <ostream>
class Winery
{
public:
staticconstchar YOUR_NAME[]; // used for printing out programmer's name
Winery(constchar * const name, constchar * const location, constint acres, constint rating);
virtual ~Winery(void);
// complete implementations for the following 4 functions, nothing needed in the .cpp file
constchar * const getName() const { return name; }
constchar * const getLocation() const { return location; }
constint getAcres() const { return acres; }
constint getRating() const { return rating; }
// print out column headings for lists of wineries, as specified by lab1output.txt
staticvoid displayColumnHeadings(std::ostream& out);
// print out a winery, as specified by lab1output.txt
friend std::ostream& operator<<(std::ostream& out, Winery *w);
private:
char *name;
char *location;
int acres;
int rating;
};
You seem to be using a lot of C-style strings. The STL std::string will reduce the codes complexity. It'll also mean avoiding direct confrontation with pointers since std::string handles them internally.
Access violations are caused by dereferencing a null pointer. Before using a pointer, check for null before using it.