Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

What is wrong with this copy constructor for a very basic singly linked list?

Without the copy constructor, my program works fine. I can declare as many empty or singleton strSets as I want and output their value without a problem in my main program. As soon as I put in the copy constructor, if I declare more than 2 sets, i get Segmentation Fault. Also, if I declare two sets and try to output the value of either, I get Segmentation Fault. If theres only one set declare it works fine. I'll need the copy constructor for when I implement the rest of my program obv, but if it doesnt work here, but it would work later on either. Heres the code:

--------------------------------------------------------------------------------

**Here is the implementation of the class strSet.h**

#ifndef _STRSET_

#define _STRSET_

#include <iostream>

#include <vector>

#include <string>

struct node {

std::string s1;

node * next;

};

class strSet {

private:

node * first;

public:

strSet (); // Create empty set

strSet (std::string s); // Create singleton set

strSet (const strSet ©); // Copy constructor

// will implement destructor and overloaded assignment operator later

void output() const;

}; // End of strSet class

#endif // _STRSET_

------------------------------------------------------------------------------

**And heres the implementation strSet.cpp**

#include <iostream>

#include <vector>

#include <string>

#include "strset2.h"

using namespace std;

strSet::strSet() {

first = NULL;

}

strSet::strSet(string s) {

node *temp;

temp = new node;

temp->s1 = s;

temp->next = NULL;

first = temp;

}

strSet::strSet(const strSet& copy) {

cout << "copy-cst\n";

node *n = copy.first;

node *prev = NULL;

while (n) {

node *newNode = new node;

newNode->s1 = n->s1;

newNode->next = NULL;

if (prev) {

prev->next = newNode;

}

else {

first = newNode;

}

prev = newNode;

n = n->next;

}

}

void strSet::output() const {

if(first == NULL) {

cout << "Empty set\n";

}

else {

node *temp;

temp = first;

while(1) {

cout << temp->s1 << endl;

if(temp->next == NULL) break;

temp = temp->next;

}

}

}

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    First off seems like there are some terminology issues here.

    "strSet (std::string s); // Create singleton set"

    this function is not creating a singleton, see first link in sources

    Your copy function makes reference to prev.... a singly linked list doesn't have prev nor does your Node struct.

    Ok I think what you mean by PREV is actually LAST i.e. the last copied node. change the name for

    clarity.

    I would also rename copy to copyMe or original in your copy constructor

    So you are faulting cause of null pointer error.

    in your print function while(1) should be while(temp)

    remove the if statement as temp will be checked by the while loop

    in your copy function i don't know that this line is needed newNode->next = NULL;

    in node *n = copy.first; if copy is null you will get seg fault

    can you show how you are using this copy function?

Still have questions? Get your answers by asking now.