-1

コードのこのセクションから無効なヌル ポインター エラーが発生しています。文字列に関係していると推測していますが、最近知ったばかりなので、問題を見つけることができません。

string batchCipherFiles()
{
int count(0);
string text, file; 
ifstream inputFile;

cout << " How many files do you wish to encrypt?: ";
cin >> count;
for (int i(1) ; i <= count ; ++i)
{
    stringstream ss;
    ss << "unencrypted" << i << ".txt";
    file = ss.str();
    inputFile.open(file.c_str(), ios::in);
    if (inputFile.fail()) 
    {
        cout << "\n An error has occurred.";
    }
    else
    {
        while (!inputFile.eof())
        {
            getline(inputFile, text);
            cout << "\n " << file << " = " << text << "\n\n";
            int applyCeasarShift(string,string);        
            applyCeasarShift(text, file);
        }
        inputFile.close();
    }
}

return (0);
}

xstring からのデバッグ行は次のとおりです。

 #ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
{   // report error and die
    if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, message)==1)
    {
        ::_CrtDbgBreak();
    }
}

助けてくれてありがとう。

編集: Return ステートメントでエラーが発生し、xstring からの抽出では、黄色の矢印が "::_CrtDbgBreak();" を指しています。

編集2:

xstring:
basic_string(const _Elem *_Ptr)
    : _Mybase()
    {   // construct from [_Ptr, <null>)
    _Tidy();
    assign(_Ptr);
    }

xutility:
template<class _Ty> inline
void _Debug_pointer(const _Ty *_First, _Dbfile_t _File, _Dbline_t _Line)
{   // test iterator for non-singularity, const pointers
if (_First == 0)
    _DEBUG_ERROR2("invalid null pointer", _File, _Line);
}

stdthrow:
#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
{   // report error and die
    if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, message)==1)
    {
        ::_CrtDbgBreak();
    }
}

うまくいけば、これは正しいものです。

4

2 に答える 2

5

問題はstring、の戻り値の型ですbatchCipherFilesが、返されていることです。return (0);戻り値の型を変更するか、 a を返しますstring

return (0);が暗黙的に に変換されstd::string((char*)0);、クラッシュが発生すると思います。std::stringコンストラクターのドキュメントには次のように記載されています。

s が指すヌル終了文字列の内容で文字列を構築します。文字列の長さは、最初のヌル文字によって決まります。s は NULL ポインターであってはなりません。

于 2012-04-29T19:22:34.163 に答える
1

return ステートメントでエラーが発生している場合は、他のコードが戻り値をポインター値として使用している可能性があります。C++ 標準では、ポインターに割り当てられたときに 0 が NULL と同じように扱われることが指定されているため、おそらくそれが問題です。

于 2012-04-29T19:22:34.003 に答える