2

一切正常,直到编译器尝试执行 push_back 操作。在 if 条件中返回正确的值。
我已将项目声明为:

vector<int> items; // inside the header file.

//在.cpp文件中

void MsPs::findnSort()
{
    for(int i = 1; i<50 ; i++)
    {

        string temp = static_cast<ostringstream*>( &(ostringstream() << i) )->str();    // TO convert int i to a string temp
        if(findSupport(temp) >= MIS[i])
        {
            items.push_back(i);
        }

    }

}

弹出以下错误:

Unhandled exception at 0x5052ad4a (msvcp100d.dll) in PrefixScan.exe: 0xC0000005: Access violation reading location 0x3d4cccd1.

PS:我还有一个使用 push_back 操作的功能,它工作正常。

谁能帮我这个?

即使这给出了同样的错误:

void MsPs::findnSort()
{
    for(int i = 1; i<50 ; i++)
    {

        items.push_back(i);
    }


}
4

1 回答 1

2

我认为问题是当静态转换返回时 ostringstream 被破坏。str()因此,您的指针在被调用时悬空。试试这个:

void MsPs::findnSort()
{
    for(int i = 1; i<50 ; i++)
    {
        ostringstream blah;
        string temp = (blah << i).str();

        if(findSupport(temp) >= MIS[i])
        {
            items.push_back(i);
        }

    }

}
于 2013-02-16T00:32:17.223 回答