Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could you please help me to find out what i'm doing wrong? I think there is a problem with a copy constructor or operator= #include

image text in transcribedCould you please help me to find out what i'm doing wrong? I think there is a problem with a copy constructor or operator=

#include

#include

#include "studentRoll.h"

#include

StudentRoll::StudentRoll() {

std::cout

head = tail = NULL;

}

void StudentRoll::insertAtTail(const Student &s) {

std::cout

Node *newNode = new Node;

Node *endNode = new Node;

newNode->s = new Student (s);

endNode = head;

//std::cout s->toString();

if(endNode == NULL){

head = newNode;

tail = newNode;

newNode->next = NULL;

std::cout

}

else{

while(endNode->next!=NULL){

endNode=endNode->next;

}

endNode->next = new Node;

endNode->next = newNode;

tail = newNode;

newNode->next = NULL;

std::cout

}

delete newNode;

delete endNode;

}

std::string StudentRoll::toString() const {

std::cout

std::string oss;

Node* tempNode = NULL;

tempNode = head;

oss = "[";

while(tempNode != NULL){

// std::cout s->toString();

std::cout

oss += tempNode->s->toString();

std::cout

tempNode = tempNode->next;

if (tempNode != NULL){

oss += ",";

}

}

oss+= "]";

std::cout

delete tempNode;

return oss;

}

StudentRoll::StudentRoll(const StudentRoll &orig) {

std::cout

Node* origChainPtr = NULL;

origChainPtr = orig.head;

if( origChainPtr == NULL){

head = NULL;

}

else{

head = new Node;

head->s = new Student (*origChainPtr->s);

// Copy remaining nodes

Node* newChainPtr = new Node;

newChainPtr = head;

origChainPtr = origChainPtr->next;

// Get next item from original chain

while(origChainPtr != NULL){

// Get next item from original chain

Student sTemp = *origChainPtr->s;

Node* newNodePtr = new Node;

newNodePtr->s = new Student (sTemp);

newChainPtr->next = newNodePtr->next;

// Advance pointer to new last node

newChainPtr = newChainPtr->next;

// Advance original-chain pointer

origChainPtr = origChainPtr->next;

}

newChainPtr->next = NULL;

tail = newChainPtr;

delete newChainPtr;

}

delete origChainPtr;

}

StudentRoll::~StudentRoll() {

std::cout

Node* current = head;

while( current != 0 ) {

Node* next = current->next;

delete current;

current = next;

}

head = tail = NULL;

}

StudentRoll & StudentRoll::operator =(const StudentRoll &right ) {

// The next two lines are standard, and you should keep them.

// They avoid problems with self-assignment where you might free up

// memory before you copy from it. (e.g. x = x)

std::cout

if (&right == this)

return (*this);

// TODO... Here is where there is code missing that you need to

// fill in...

Node *curent = right.head;

if (curent == NULL){

curent = NULL;

}

else{

Node *newNode = new Node;

this->~StudentRoll();

while( curent != NULL){

newNode->s = new Student (*curent->s);

newNode->next = new Node;

newNode->next = curent->next;

curent = curent->next;

newNode = newNode->next;

}

newNode->next = NULL;

tail = newNode;

delete newNode;

}

delete curent;

//head = curent;

// KEEP THE CODE BELOW THIS LINE

// Overloaded = should end with this line, despite what the textbook says.

return (*this);

}

PASSED: s1.getName) at testStudent03.cpp line: 27 PASSED: s1.getPerm() at testStudent03.cpp line: 28 PASSED: s2.getName) at testStudent03.cpp line: 29 PASSED: s2.getPerm() at testStudent03.cpp line: 30 PASSED: s3.getName) at testStudent03.cpp line: 34 PASSED: s3.getPerm() at testStudent03.cpp line: 35 /testStudentRoll00 Running tests from: testStudentRoll00.cpp in costructor in toString [1PASSED sr.toString() at testStudentRoll00.cpp line: 12 1n StudentROLL destructor /testStudentRoll01 Running tests from: testStudentRol101.cpp PASSED: s.getName() at testStudentRoll01.cpp line: 13 PASSED: s.getPerm() at testStudentRoll01.cpp line: 14 in costructor in toString [1PASSED sr.toString() at testStudentRoll01.cpp line: 17 in insertAtTail in insertAtlail done if in toString in toString WHILE make: [Makefile:16: tests] Segmentation fault (core dumped)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke, David Auer

11th Edition

B003Y7CIBU, 978-0132302678

More Books

Students also viewed these Databases questions

Question

What is the difference between a hub and a layer 2 switch?

Answered: 1 week ago

Question

Be familiar with the five basic ways to manage demand.

Answered: 1 week ago