Given the code in Main:
// main.cpp
wineries->insert(winery("Lopez Island Vinyard", "San Juan Islands", 7, 95));
Two things will happen: The winery ctor is invoked where I have intialized the winery private members:
//winery.cpp
winery::winery(const char * const name, const char * const location,
const int acres, const int rating)
: name( new char[strlen(name)+1] )
, location( new char[strlen(location)+1] )
, acres( 0 ), rating( 0 )
{
}
When it is finished the result of the 'this' pointer has garbage value. Why is this?? I am not intializing incorrectly..
Second thing that happens, After the winery ctor dies we go to the list::insert( const winery &winery ) function:
void list::insert(const winery& winery)
{
node *NodePtr = new node( winery );
// NodePtr->item has the garbage.
NodePtr->item = winery;
}
list::node::node( const winery& winery )
{
// This works because I have a default ctor for the winery object
// and ONLY for that reason...
//How can I use the node ctor without having to use a default ctor for the winery class?
}
Why am I getting garbage as a result of the values passed to the winery ctor? The winery public member functions are as follows, where name, location, acres, and rating are all private members to the winery class.
winery::winery()
{
// do nothing dfault ctor
// only here so I can add the &winery to the node ctor..
}
winery::~winery()
{
delete location;
delete name;
// your code here
}
const char * const winery::getName() const
{
//winery *wine_t = new winery();
const char cName[5] = "four";
// just to see if It still gives garbage..
return cName
}
const char * const winery::getLocation() const
{
// return one of winery's private members.
// It WILL Crash when this function is called.
// THAT might be the issue with **garbage values**
return location;
}
Without these functions have paramters, it makes it difficult to transfer the attributes over to a wineryPtr object and then it would be logical to add the entire winery object to the linkedlist......
// list.h
#ifndef _LIST_
#define _LIST_
#include <ostream>
#include "winery.h"
using namespace std;
class list
{
public:
list(void); // constructor
virtual ~list(void); // destructor
...
void insert(const winery& winery);
winery * const find(const char * const name) const;
private:
struct node
{
node(const winery& winery); // constructor
winery item;
node * nextByName;
node * nextByRating;
};
node * headByName;
node * headByRating;
};
#endif // _LIST_
YEAH! So my questions are a little scatterd, and I hope someone out their has the time to help!