Учитывая код в основной:Мусор Результат! C++ LinkedList
// main.cpp
wineries->insert(winery("Lopez Island Vinyard", "San Juan Islands", 7, 95));
две вещи случатся:
Конструктор винзавод вызывается, когда я intialized винодельни частных пользователей:
//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) { }
Когда он будет завершен, результат указателя
this
имеет значение для мусора. Почему это? Я не правильно инициализирую?После того, как конструктор винзавод умирает, мы идем к функции
list::insert(const winery &winery)
: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 constructor for the winery object // and *only* for that reason... // How can I use the node constructor without having to use a default constructor for the winery class? }
Почему я получаю мусор в результате значений, переданных конструктору Виннице?
винодельни функции-члены являются, где
name
,location
,acres
иrating
все частные члены к классу винодельня.winery::winery() { // do nothing default constructor // only here so I can add the &winery to the node constructor.. } 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; }
Без этих функций с параметрами, что делает его трудно передать атрибуты к объекту wineryPtr, и тогда было бы логично, чтобы добавить весь объект винного завода в 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_
Мои вопросы немного рассеяны, и я надеюсь, что у кого-то есть время, чтобы помочь!
Пожалуйста, прикладывать больше усилий задавать ваш вопрос правильно. Я гарантирую, что вы получите лучший ответ –
Где на земле этот указатель, о котором вы говорите? – Zed
Я не понимаю, что вы имеете в виду? Что вы понимаете под мусорной ценностью, и как вы определяете, что это мусор? Покажите некоторый код, который воспроизводит проблему, включая показ того, где вы получаете данные мусора, и как вы проверяете, что это мусор. – jalf