Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4078480
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:40:47+00:00 2026-05-20T17:40:47+00:00

Last week, we created a program that manages sets of strings, using classes and

  • 0

Last week, we created a program that manages sets of strings, using classes and vectors. I was able to complete this 100%. This week, we have to replace the vector we used to store strings in our class with simple singly linked lists.

The function basically allows users to declare sets of strings that are empty, and sets with only one element. In the main file, there is a vector whose elements are a struct that contain setName and strSet (class).

HERE IS MY PROBLEM: It deals with the copy constructor of the class. When I remove/comment out the copy constructor, I can declare as many empty or single sets as I want, and output their values without a problem. But I know I will obviously need the copy constructor for when I implement the rest of the program. When I leave the copy constructor in, I can declare one set, either single or empty, and output its value. But if I declare a 2nd set, and i try to output either of the first two sets, i get a Segmentation Fault. Moreover, if i try to declare more then 2 sets, I get a Segmentation Fault. Any help would be appreciated!!

Here is my code for a very basic implementation of everything:

Here is the setcalc.cpp: (main file)

#include <iostream>
#include <cctype>
#include <cstring>
#include <string>
#include "strset2.h"

using namespace std;

// Declares of structure to hold all the sets defined
struct setsOfStr {
    string nameOfSet;
    strSet stringSet;
};

// Checks if the set name inputted is unique
bool isSetNameUnique( vector<setsOfStr> strSetArr, string setName) {
    for(unsigned int i = 0; i < strSetArr.size(); i++) {
        if( strSetArr[i].nameOfSet == setName ) {
            return false;
        }
    }
    return true;
}

int main() {
    char commandChoice;
    // Declares a vector with our declared structure as the type
    vector<setsOfStr> strSetVec;

    string setName;
    string singleEle;
    // Sets a loop that will constantly ask for a command until 'q' is typed
    while (1) {
            cin >> commandChoice;
        // declaring a set to be empty
        if(commandChoice == 'd') {
            cin >> setName;
            // Check that the set name inputted is unique
            if (isSetNameUnique(strSetVec, setName)  == true) {
                strSet emptyStrSet;
                setsOfStr set1;
                set1.nameOfSet = setName;
                set1.stringSet = emptyStrSet;
                strSetVec.push_back(set1);
            }
            else {
                cerr << "ERROR: Re-declaration of set '" << setName << "'\n";
            }
        }
        // declaring a set to be a singleton
        else if(commandChoice == 's') {
            cin >> setName;
            cin >> singleEle;
            // Check that the set name inputted is unique
            if (isSetNameUnique(strSetVec, setName) == true) {
                strSet singleStrSet(singleEle);
                setsOfStr set2;
                set2.nameOfSet = setName;
                set2.stringSet = singleStrSet;
                strSetVec.push_back(set2);
            }
            else {
                cerr << "ERROR: Re-declaration of set '" << setName << "'\n";
            }
        }
        // using the output function
        else if(commandChoice == 'o') {
            cin >> setName;
            if(isSetNameUnique(strSetVec, setName) == false) {
                // loop through until the set name is matched and call output on its strSet
                for(unsigned int k = 0; k < strSetVec.size(); k++) {
                    if( strSetVec[k].nameOfSet == setName ) {
                            (strSetVec[k].stringSet).output();
                    }
                }
            }
            else {
                cerr << "ERROR: No such set '" << setName << "'\n";
            }
        }
        // quitting
        else if(commandChoice == 'q') {
            break;
        }
        else {
            cerr << "ERROR: Ignoring bad command: '" << commandChoice << "'\n";
        }
    }
    return 0;
}

Here is the strSet2.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); // Copy constructor
    // will implement destructor and overloaded assignment operator later

    void output() const;


};  // End of strSet class

#endif  // _STRSET_

And here is the strSet2.cpp (implementation of class)

#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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:40:48+00:00Added an answer on May 20, 2026 at 5:40 pm

    this looks a bit peculiar:

    strSet::strSet(string s) {
        node *temp;
        temp = new node;
        temp->s1 = s;
        temp->next = NULL;
        first = temp;
    }
    

    what if ‘first’ is pointing to something already? You are then effectively killing the previous list and causing a mem leak.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just began using Mongoid last week. I am running into this association problem
I was writing some Unit tests last week for a piece of code that
Given a week-day (1-7), how can I calculate what that week-day's last date was?
I am contemplating writing a program that will move some newly created dirs to
I observed something last week that I did not expect, and will describe below.
I've spent the last week developing code to connect to a Web Service using
I take This Week's Revenue and Last Week's Revenue values from the server and
Ok, Last time I posted this (last week), I didn't describe the problem correctly.
I have a weird behaviour that has only shown up in the last week.
With help from the community last week, I successfully implemented this code here Now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.