주에서 코드를 감안할 때 :가비지 결과! C++ LinkedList의
// main.cpp
wineries->insert(winery("Lopez Island Vinyard", "San Juan Islands", 7, 95));
일어날 것 두 가지 :
//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)
{
}
:
와이너리 생성자는 내가 와이너리 개인 회원 intialized 한 곳 호출 작업이 끝나면 포인터
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