2013-10-02 3 views
-2

계속 오류가 발생합니다. 나는 이유를 모른다. 몇 줄마다 "ok"문을 인쇄하여 오류 위치를 알려주려고했지만 첫 번째 오류도 인쇄하기 전에 오류가 발생합니다. 나는 왜 심지어세그 폴트 : 알 수없는 원인

#include <iostream> 
#include <cstdlib> 
#include <vector> 
using namespace std; 

class isprime{ 
    public: 
     isprime(); 
     void start(int thing); 
     bool test(int target); 
     void check(int x); 
     void checktwo(int xtwo); 

     int operator()(int p); 

    private: 
     void path(int targ); 
     vector<int> testing; 
     int b; 
}; 

int main() 
{ 
    int given; 

    while(cin>>given) 
    { 
     isprime Begin; 
     Begin.start(given);//check input as long as there is input 
    } 
    return 0; 
} 

void isprime::start(int thing) 
{ 
    if(test(thing) == 1) 
    { 
     cout<<thing<<" is a prime number."; 
    } 
    else 
    { 
     check(thing); 
    } 
} 

isprime::isprime() 
{ 
    testing[0] = {2}; 
    b = 0; 
} 

void isprime::check(int x)//checks if input is prime, and sets up next step if so 
{ 
    int s; 
    if(x == 0 || x == 1 || x == -1 || x == 2 || x == -2) 
    { 
     cout<<x<<" is a prime number."; 
    } 
    else 
    { 
     for(int i = 2; i < x; i++) 
     { 
      s = x % i; 
      if(s == 0) 
      { 
       b = 1; 
       break; 
      } 
     } 
     if(s != 0) 
     { 
      cout<<x<<" is a prime number."; 
     } 
     path(x); 
    } 
} 

bool isprime::test (int target)//see if input is already in list 
{ 
    for(int i = 0; i < testing.size(); i++) 
    { 
     if(target == testing[i]) 
     { 
      return 1; 
     } 
    } 
    if(int i = testing.size() && target != testing[i]) 
    { 
     return 0;//if not in list, must test whether it is prime 
    } 
} 

void isprime::path(int targ) 
{ 
    int y = testing.back() + 1; 
    while(y != targ)//find all primes between list end and input 
    { 
     checktwo(y); 
     y++; 
    } 

    testing.push_back(targ);//add prime input to vector 

    int storage = testing.size();//remember size 
    int z = targ + 1; 

    while(b = 1)//find the next prime while the target isn't prime 
    { 
     checktwo(z); 
     if(testing.size() != storage)//if the size changed, the next prime has been found 
     { 
      break; 
     } 
     z++; 
    } 
} 
void isprime::checktwo(int xtwo)//modified check function to add prime numbers between the vector end and the input to the vector 
{ 
    int s; 
    if(xtwo == -2 || xtwo == -1 || xtwo == 0 || xtwo == 1 || xtwo == 2) 
    { 
     testing.push_back(xtwo); 
    } 
    else 
    { 
     for(int i = 2; i < xtwo; i++) 
     { 
      s = xtwo % i; 
      if(s == 0) 
      { 
       break; 
      } 
     } 
     if(s != 0) 
     { 
      testing.push_back(xtwo); 
     } 
    } 
} 

int operator()(int p) 
{ 
    test(p);//calls a private member function to expand list of prime numbers (test) 
} 
다음
+1

디버거에서 실행 해 보셨습니까? –

+1

이것은 나를 위해 컴파일되지 않습니다. 디버거는 친구입니다. –

+0

1은 ** 아닙니다 ** 소수입니다. 그러나 귀하의 질문에 대한 답변이 아닙니다. – taskinoor

답변

5

이의 하나 "확인"문을 인쇄하지 않습니다이 혹은 함수 호출의 일부 사이, 운영자 과부하 가능성이 붙어지고 있지만 설명 할 것이라고 생각 문제 :

isprime::isprime() 
{ 
    testing[0] = {2}; //<<---------- here 
    b = 0; 
} 

"testing"의 크기는 0입니다. 테스트가 std::vector 인 경우 생성자에서 크기를 초기화하지 않았으므로 testing의 인덱스에 액세스하려고하면 프로그램이 중단됩니다. (글쎄, 그것은 "정의되지 않은 동작"이지만, 귀하의 경우에는 충돌 할 것입니다). 이것은 생성자에서 발생하기 때문에 "main()"을 입력하면 프로그램이 충돌합니다.

관련 문제