2012-04-29 30 views
-1

このコードセクションで無効なNULLポインタエラーが発生します。私はそれが文字列と関係があると推測していますが、私は最近それらについて学んだだけで問題を見つけることはできません。任意の助けを事前に無効なヌルポインタ - C++

#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(); 
    } 
} 

ありがとう:

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からデバッグラインがあります。

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

EDIT 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(); 
    } 
} 

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

+1

どのラインでエラーが発生しますか? – Chris

+1

スタックトレースの先頭が完全に無駄です。元の行は何ですか? –

+0

スタックトレース全体を表示してください。 '_CrtDbgBreak()'と呼ばれるもの、_that_関数と呼ばれるものなどを知る必要があります。 –

答えて

4

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

私はreturn (0);が暗黙のうちにstd::string((char*)0);に変換され、クラッシュを引き起こすと思います。 std::string constructorのドキュメントは次のように書かれています。

sで指されるヌル終了文字列の内容を持つ文字列を作成します。文字列の長さは、最初のヌル文字によって決まります。 はNULLポインタであってはなりません。

+0

私は関数をintに変更して動作します!ありがとう、非常に迅速な返信もありがとう。 – user1364552

+0

@ user1364552:値を返す必要がない場合は、 'void'を使います。私。 'void batchCipherFiles(){/ **** /;}戻る; } ' – MSalters

1

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

+0

正しい: "その他のコード"は 'string :: string(const char *)' – MSalters

関連する問題