Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating an empty instance of a class
    primarykey
    data
    text
    <p>Ok, I have a class <code>Set</code> who keeps a <code>vector&lt;int&gt;</code> as its data payload. It has a constructor that accepts a <code>string</code> as a parameter such as <code>Set test = Set("1 2 3 4 5 6");</code> I have a function that reads the line, and parses it into a <code>vector&lt;int&gt;</code> from there I can perform operations on the <code>Set</code>. The problem comes when <code>Set test = Set("");</code> is called, my constructor cannot make a <code>Set</code> because it has nothing to parse. My program can't go anywhere. I've tried putting if else statements in the constructor but how can I declare an empty <code>set</code>?</p> <p>Right now I get a segmentation fault.</p> <pre><code>#include "Set.h" using namespace std; /*************************************************************************************** * Constructors and Destructors **/ Set::Set(){ } Set::Set(string inputString) { if(inputString != ""){ readLine(inputString); } else{ //I've tried several things here, none of which work. } } Set::~Set(){ } /*************************************************************************************** * readLine function * * This function takes in a string, takes the next int, and stores in in a vector. * * Parameters: string, the string to be parsed. * **/ void Set::readLine(string inString){ ScanLine scanLine; scanLine.openString(inString); while(scanLine.hasMoreData()){ addToSet(scanLine.nextInt()); } } /*************************************************************************************** * addToSet function * * This function takes in a int that is an element and adds it to "this" Set. * * Parameters: int, the element to be added. * Return: int, the value that was added. * **/ int Set::addToSet(int element){ int returnValue = -1; if(!containsElement(element)){ this-&gt;theSet.push_back(element); returnValue = element; } return returnValue; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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