0
votes

No error

// list.h
//...
list::node::node(const winery &winery)
{
}
void list::insert(const winery& winery)
{
    //const char *Names = winery.getName();
    node *new_node = new node( winery );
}

Adding the list::node::node(const winery &winery ) function to the list.cpp file enables allocation of the node.

however, it forces me to add a dfault constructor to the winery class:

class winery
{
public :
    winery(const char * const name, const char * const location, 
const int acrces, const int rating);
    winery();
    virtual ~winery(void);
//...

And thereby havinvg to provide the body of the default ctor. Is there a way to have it work without declaring and defining a default ctor to the winery class?

3
Please select your code, and click the button with the 1's and 0's on it. This will fix the formatting. And then set the tags correctly. "none" is not what your question is about. Finally, if this is a homework question, you should tag is "homework"1800 INFORMATION
what button with 1's and zeros?user40120
@lampshade, while you edit your answer you can format it by selecting fragments and pressing the buttons on the menu. It's better to remove your HTML tags. If you open a tag <pre> you close it with </pre> not </code>.Nick Dandoulakis
When you edit the post, there is a toolbar just above the edit window. One of the buttons has some 1's and 0's on it.1800 INFORMATION
Please give some details on the node struct. Does it have a construtor? If you can post the definition of node, that would help. Also, uncomment the code which is causing the error, compile it, then post the erroring code exactly as it is in your source, and post the error text that you are getting. Posting the text of the error is REALLY helpful for the more experienced coders who have seen these errors many times before.A. Levy

3 Answers

2
votes

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.

2
votes

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.

0
votes

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.