2013-10-20 4 views
-2

좋아, 클래스 Setvector<int>을 데이터 페이로드로 유지합니다. 그것은 Set test = Set("1 2 3 4 5 6");과 같은 매개 변수로 string을 받아들이는 생성자를 가지고 있습니다. 저는이 라인을 읽는 함수를 가지고 있고 거기에서 vector<int>으로 구문 분석하여 Set에서 연산을 수행 할 수 있습니다. 문제는 Set test = Set("");이 호출 될 때 발생합니다. 구문 분석 할 것이 없으므로 내 생성자는 Set을 만들 수 없습니다. 내 프로그램은 아무데도 갈 수 없다. 나는 생성자에 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; 
} 
+1

게시 한 코드에 어떤 문제가 있습니까? 왜 당신은 당신이'// 나는 여러 가지를 시도했으나 어떤 것도 작동하지 않았다 '고 생각할 수 있습니다. – Oswald

+0

else 문을 끝내면 세그먼트 오류 11이 발생합니다. 그러면 다른 부분이 필요하다고 생각하게되었습니다. –

+0

게시 한 코드는 세분화 오류에 대해 책임지지 않습니다. [짧고, 독립적 인, 올바른 예] (http://sscce.org)를 제공하십시오. – Oswald

답변

0

해결책을 찾았습니다. 내 균등 함수에서 if(set1.size() != set2.size())을 가져야했을 때 if(!set1.size() == set2.size())을 가졌습니다. 어떤 이유로 인해 세그먼트 오류가 발생했습니다.

관련 문제