Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help with copy constructor for very basic implementation of singly linked lists
    primarykey
    data
    text
    <p>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. </p> <p>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).</p> <p>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!!</p> <p>Here is my code for a very basic implementation of everything:</p> <p><strong>Here is the setcalc.cpp: (main file)</strong></p> <pre><code>#include &lt;iostream&gt; #include &lt;cctype&gt; #include &lt;cstring&gt; #include &lt;string&gt; #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&lt;setsOfStr&gt; strSetArr, string setName) { for(unsigned int i = 0; i &lt; 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&lt;setsOfStr&gt; strSetVec; string setName; string singleEle; // Sets a loop that will constantly ask for a command until 'q' is typed while (1) { cin &gt;&gt; commandChoice; // declaring a set to be empty if(commandChoice == 'd') { cin &gt;&gt; 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 &lt;&lt; "ERROR: Re-declaration of set '" &lt;&lt; setName &lt;&lt; "'\n"; } } // declaring a set to be a singleton else if(commandChoice == 's') { cin &gt;&gt; setName; cin &gt;&gt; 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 &lt;&lt; "ERROR: Re-declaration of set '" &lt;&lt; setName &lt;&lt; "'\n"; } } // using the output function else if(commandChoice == 'o') { cin &gt;&gt; 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 &lt; strSetVec.size(); k++) { if( strSetVec[k].nameOfSet == setName ) { (strSetVec[k].stringSet).output(); } } } else { cerr &lt;&lt; "ERROR: No such set '" &lt;&lt; setName &lt;&lt; "'\n"; } } // quitting else if(commandChoice == 'q') { break; } else { cerr &lt;&lt; "ERROR: Ignoring bad command: '" &lt;&lt; commandChoice &lt;&lt; "'\n"; } } return 0; } </code></pre> <p><strong>Here is the strSet2.h:</strong></p> <pre><code>#ifndef _STRSET_ #define _STRSET_ #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; 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 &amp;copy); // Copy constructor // will implement destructor and overloaded assignment operator later void output() const; }; // End of strSet class #endif // _STRSET_ </code></pre> <p><strong>And here is the strSet2.cpp (implementation of class)</strong></p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; #include "strset2.h" using namespace std; strSet::strSet() { first = NULL; } strSet::strSet(string s) { node *temp; temp = new node; temp-&gt;s1 = s; temp-&gt;next = NULL; first = temp; } strSet::strSet(const strSet&amp; copy) { cout &lt;&lt; "copy-cst\n"; node *n = copy.first; node *prev = NULL; while (n) { node *newNode = new node; newNode-&gt;s1 = n-&gt;s1; newNode-&gt;next = NULL; if (prev) { prev-&gt;next = newNode; } else { first = newNode; } prev = newNode; n = n-&gt;next; } } void strSet::output() const { if(first == NULL) { cout &lt;&lt; "Empty set\n"; } else { node *temp; temp = first; while(1) { cout &lt;&lt; temp-&gt;s1 &lt;&lt; endl; if(temp-&gt;next == NULL) break; temp = temp-&gt;next; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload