Я пытаюсь реализовать проблему, подобную "facebook".
Я создал класс с именем User
.У пользователя есть список друзей.
Я пробовал это с использованием вектора C ++, и это работало без проблем.
Затем я попытался изменить вектор на LinkedList
, используя шаблонный классУ меня есть.
В шаблоне есть конструктор копирования и деструктор.
Я протестировал и отладил шаблон для других типов данных.
class User
{
private:
string uname;
//vector<User> myfriends;
LinkedList<User> myfriends;
public:
User() { uname = "none"; }
User(string n) { uname = n; }
string getName() { return uname; }
void addFriend(User &u)
{
//add u to me
myfriends.appendNode(u);
//add "me" to u
u.myfriends.appendNode(*this); //causes problem?
//myfriends.push_back(u); //when using vector
//u.myfriends.push_back(*this); //works when using vector
}
void listFriends()
{
cout << uname << " has " << myfriends.getSize() << " friends" << endl;
myfriends.displayList(); //prints values in linked list
}
friend ostream& operator<< (ostream& out, User u)
{
out << u.uname;
return out;
}
};
Я хочу, чтобы функция addFriendустановить «взаимное» соединение.
Это работает, когда я использую vector
, но при использовании LinkedList
и этой программы тестирования:
User u1("joe");
User u2("sam");
u1.addFriend(u2);
u1.listFriends();
Я получаю правильный вывод
joe has 1 friends
sam
Однако я также получаю ошибку во время выполнения, которая говорит мне, что с моими указателями происходит что-то странное.
"Из-за проблемы программа перестала работать правильно."
Я использую Visual Studio Express 2017.
Я пытаюсь выяснить, есть ли какой-то базовый недостаток при создании соединений таким образом, пытаясь нарисовать некоторые картинки, чтобы разобраться.
Любые мысли о том, что может бытьИспользование ошибки времени выполнения?
Вот функция displayList()
:
template <class T>
void LinkedList<T>::displayList()
{
//"walk" the list and print each value
ListNode *nodePtr;
//to walk the list
//start at the beginning
nodePtr = head;
//while there is a node to print
while (nodePtr) {
//display the value
cout << nodePtr->data << endl;
//move to next node
nodePtr = nodePtr->next;
}
}
Вот код displayList в шаблоне LinkedList
template <class T>
void LinkedList<T>::displayList()
{
//"walk" the list and print each value
ListNode *nodePtr; //to walk the list
//start at the beginning
nodePtr = head;
//while there is a node to print
while (nodePtr)
{
//display the value
cout << nodePtr->data << endl;
//move to next node
nodePtr = nodePtr->next;
}
}
Вот это appendNode
template <class T>
void LinkedList<T>::appendNode(T value)
{
ListNode *newNode; //to point to a new node
ListNode *nodePtr; //to move through the list
//allicate a new node and store value
newNode = new ListNode;
newNode->data = value;
newNode->next = nullptr;
//if list is empty make this the first node
if (!head)
head = newNode;
else // insert at end of list
{
//initialize nodePtr to head of list
nodePtr = head;
//"walk" the listt to find the last node
while (nodePtr->next) //if not null this is true
{
nodePtr = nodePtr->next;
}
//nodePtr now points to last node in list
//add the new node
nodePtr->next = newNode;
//remember it's next has already been assigned to null
}
numElements++;
}
Вот ссылка https://repl.it/@prprice16/GrowlingFastRule