Skip to content

Commit e13bf39

Browse files
committed
implemented task 5 in theory, it should work, but right now throws seg fault error, further debug is needed.
1 parent cf586a0 commit e13bf39

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

src/chatlogic.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ ChatLogic::ChatLogic()
1818
////
1919

2020
// create instance of chatbot
21-
_chatBot = new ChatBot("../images/chatbot.png");
21+
//_chatBot = new ChatBot("../images/chatbot.png");
2222

2323
// add pointer to chatlogic so that chatbot answers can be passed on to the GUI
24-
_chatBot->SetChatLogicHandle(this);
24+
//_chatBot->SetChatLogicHandle(this);
2525

2626
////
2727
//// EOF STUDENT CODE
@@ -33,7 +33,7 @@ ChatLogic::~ChatLogic()
3333
////
3434

3535
// delete chatbot instance
36-
delete _chatBot;
36+
//delete _chatBot;
3737

3838
// No need to manually delete edges now.
3939
// The child nodes' unique_ptr<GraphEdge> will handle that.
@@ -195,26 +195,28 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename)
195195

196196
// identify root node
197197
GraphNode *rootNode = nullptr;
198-
for (auto &node : _nodes)
198+
for (auto it = std::begin(_nodes); it != std::end(_nodes); ++it)
199199
{
200200
// search for nodes which have no incoming edges
201-
if (node->GetNumberOfParents() == 0)
201+
if ((*it)->GetNumberOfParents() == 0)
202202
{
203203

204204
if (rootNode == nullptr)
205205
{
206-
rootNode = node.get(); // assign current node to root
206+
rootNode = (*it).get(); // assign current node to root
207207
}
208208
else
209209
{
210210
std::cout << "ERROR : Multiple root nodes detected" << std::endl;
211211
}
212212
}
213213
}
214-
214+
ChatBot chatbot("../images/chatbot.png");
215+
SetChatbotHandle(&chatbot);
215216
// add chatbot to graph root node
216-
_chatBot->SetRootNode(rootNode);
217-
rootNode->MoveChatbotHere(_chatBot);
217+
chatbot.SetChatLogicHandle(this);
218+
chatbot.SetRootNode(rootNode);
219+
rootNode->MoveChatbotHere(std::move(chatbot));
218220

219221
////
220222
//// EOF STUDENT CODE

src/graphnode.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ void GraphNode::AddEdgeToChildNode(std::unique_ptr<GraphEdge> edge)
3535

3636
//// STUDENT CODE
3737
////
38-
void GraphNode::MoveChatbotHere(ChatBot *chatbot)
38+
void GraphNode::MoveChatbotHere(ChatBot chatbot)
3939
{
40-
_chatBot = chatbot;
41-
_chatBot->SetCurrentNode(this);
40+
_chatBot = std::move(chatbot);
41+
_chatBot.SetCurrentNode(this);
4242
}
4343

4444
void GraphNode::MoveChatbotToNewNode(GraphNode *newNode)
4545
{
46-
newNode->MoveChatbotHere(_chatBot);
47-
_chatBot = nullptr; // invalidate pointer at source
46+
newNode->MoveChatbotHere(std::move(_chatBot));
47+
//_chatBot = nullptr; // invalidate pointer at source
4848
}
4949
////
5050
//// EOF STUDENT CODE

src/graphnode.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class GraphNode
2020

2121
// data handles (not owned)
2222
std::vector<GraphEdge *> _parentEdges; // edges to preceding nodes
23-
ChatBot *_chatBot;
23+
ChatBot _chatBot;
2424

2525
////
2626
//// EOF STUDENT CODE
@@ -49,7 +49,7 @@ class GraphNode
4949
//// STUDENT CODE
5050
////
5151

52-
void MoveChatbotHere(ChatBot *chatbot);
52+
void MoveChatbotHere(ChatBot chatbot);
5353

5454
////
5555
//// EOF STUDENT CODE

0 commit comments

Comments
 (0)