C++ Seg故障:原因不明

C++ Seg故障:原因不明,c++,C++,我一直在犯seg错误。我不知道为什么。尝试每隔几行打印一条“ok”语句,以显示错误所在,但它在打印出第一条语句之前会将错误分段。我认为它可能被困在操作符重载中,或者在一些函数调用之间,但这并不能解释为什么它甚至没有打印出一条“ok”语句 #include <iostream> #include <cstdlib> #include <vector> using namespace std; class isprime{ public:

我一直在犯seg错误。我不知道为什么。尝试每隔几行打印一条“ok”语句,以显示错误所在,但它在打印出第一条语句之前会将错误分段。我认为它可能被困在操作符重载中,或者在一些函数调用之间,但这并不能解释为什么它甚至没有打印出一条“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)
}
#包括
#包括
#包括
使用名称空间std;
等级互质{
公众:
isprime();
无效开始(int thing);
布尔测试(int目标);
无效检查(INTX);
无效检查二(int xtwo);
int运算符()(int p);
私人:
无效路径(int targ);
媒介测试;
int b;
};
int main()
{
给定的int;
而(cin>>给定)
{
isprime开始;
start.start(给定);//只要有输入,就检查输入
}
返回0;
}
void isprime::start(int thing)
{
如果(测试(事物)==1)
{
cout问题是:

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

测试[0]={2};//你试过在调试器中运行它吗?这甚至不能为我编译。调试器是你的朋友。1不是素数,尽管它不是你问题的答案。