1
我已經在這裏好幾天了,我似乎無法找出爲什麼我最後兩件事不會打印。代碼很長,所以我不會發布一切,但如果你需要它,我願意提供整個源代碼。爲什麼我的打印功能不會在我的鏈接列表的任一子節點中顯示元素?
基本上我在爲每個列出的元素添加1個元素之後調用print函數。除了最後兩個配偶和孩子之外,它將全部打印出來。這兩個是最複雜的,因爲它們也是他們自己的列表。當我測試孩子的for循環時,它顯示無論我將多少個孩子添加到Vector中,它的大小都是0.這是爲什麼?
void AddressNode::PrintFull()
{
cout << setfill(' ') << endl;
cout << setw(15) << "UID " << "ID" << setfill('0') << setw(3) << id_ << setfill(' ')<< endl;
cout << setw(15) << "NAME:" << firstName_ << " " << lastName_ << endl;
cout << setw(15) << "Address1:" << address_ << endl;
cout << setw(15) << "City:" << city_<< " " << endl;
cout << setw(15) << "State:" << state_<< " " << endl;
cout << setw(15) << "Zip:" << zip_<< " " << endl;
cout << setw(15) << "Date_Birth:" << dob_<< " " << endl;
cout << setw(15) << "Date_Death:" << dod_<< " " << endl;
cout << setw(15) << "Date_Wedding:" << dow_<< " " << endl;
cout << setw(15) << "Spouse:" << (spouse_ ? spouse_->GetFirstName() : "") << " " << (spouse_ ? spouse_-> GetLastName() : "") << endl;
for(unsigned int i = 0; i < children_.size(); i++)
{
cout << setw(15) << "Child: " << i << ": " << children_[i]->GetFirstName()<< " " << children_[i]->GetLastName()<< endl;
}
}
private:
std::string firstName_;
std::string lastName_;
std::string city_ ;
std::string state_ ;
std::string zip_ ;
std::string dob_ ;
std::string dow_;
std::string dod_;
std::string address_;
std::string spouseTempString;
std::vector<AddressNode*> children_;
AddressNode* spouse_;
unsigned int id_;
void AddressNode::AddChild(AddressNode& child)
{
vector<AddressNode*>::iterator iter;
if((iter = find(children_.begin(), children_.end(), &child)) != children_.end())
return;
children_.push_back(&child);
if (spouse_)
spouse_->AddChild(child);
}
public:
AddressNode(const std::string& firstName, const std::string& lastName, int id)
: children_(), id_(id)
{
firstName_= "";
firstName_+= firstName;
lastName_="";
lastName_+= lastName;
}
請嘗試創建一個[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)並向我們顯示。推動指針會讓我感到擔憂,並且會成爲問題,例如,你調用AddChild將一個引用傳遞給一個局部變量。 –
很難看到上下文例如AddressNode *指向的對象的所有者。 –