好的,我有一个类Set
将 avector<int>
作为其数据有效负载。它有一个接受 astring
作为参数的构造函数,例如Set test = Set("1 2 3 4 5 6");
我有一个读取该行的函数,并将其解析为 avector<int>
从那里我可以对Set
. 问题来了,当Set test = Set("");
被调用时,我的构造函数无法生成,Set
因为它没有什么可解析的。我的程序哪儿也去不了。我尝试在构造函数中放置 if else 语句,但我如何声明一个空的set
?
现在我得到一个分段错误。
#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->theSet.push_back(element);
returnValue = element;
}
return returnValue;
}