Given the code above, the allocation would read:
node* new_node = new node(winery);
then it's a matter of correctly inserting new_node
into your two lists (name and rating) by iterating each separately and setting the pointers correctly.
Given the code above, the allocation would read:
node* new_node = new node(winery);
then it's a matter of correctly inserting new_node
into your two lists (name and rating) by iterating each separately and setting the pointers correctly.
I'm a little confused by this question. Why do you need to default construct a node? Use the supplied constructor that takes a winery. Like fbereton suggested in his answer.
node* aNode = new node(aWinery);
You need to use a node pointer (node*
) not a node reference (node&
) since you are going to be explicitly managing the memory in the list data structure. Also, it is not a good idea to set the head pointers to NULL at the beginning of the insert function
headByName = NULL;
headByRating = NULL;
You want to do that in the constructor. Otherwise, you lose the nodes in your list every time you insert.
But on to the error you are describing. It would really help if you post the error you are getting, but it sounds like the node(const winery&) constructor is not defined anywhere and the linker wants you to define it. It sounds like you think you are not allowed to define that constructor because the homework instructions mandate that you use the node as defined. However, I think that the instructions probably mean that you only need to use the given header definition of the node struct. That is, you are given the specifications for node, but you need to implement the details yourself.
One of these details is the constructor implementation. You don't need to modify the header to define the constructor. In one of your .cpp files (list.cpp, main.cpp, winery.cpp, it doesn't matter which) Define the following function
list::node::node(const winery& aWinery)
{
item = aWinery;
nextByName = NULL;
nextByRating = NULL;
}
Now the constructor is defined and that should resolve the link error. As a side note, a lot of your functions have this argument signature: const winery& winery
. Now I don't know exactly what the C++ standard has to say about this, but I'd guess it is illegal to have a variable and a type with the same name. Even if your compiler lets you do it, it is confusing.
To specify winery such that a default constructor is not necessary:
Option 1: Specify default values for each parameter in its constructor:
winery(const char * const name = "",
const char * const location = "",
const int acrces = -1,
const int rating = -1);
Note that if you go this route you'll have to add support code to check and make sure your winery
's data is valid.
Option 2: The other option to take is to eliminate the case that requires the default constructor. To find it/them, put the default constructor in the private
space of the winery struct, and see where the compiler emits an error. Those instances of winery
will need valid initial constructor data.