문자열과 벡터 대신 동적으로 할당 된 2 차원 문자 배열을 사용해야하는 프로그래밍 할당이 있습니다. 두 클래스가 있습니다 : char 배열에 대한 포인터를 보유하는 Word 및 Word 배열에 대한 포인터를 보유하는 WordList.배열 내의 객체에서 함수를 호출 할 때 세그먼트 오류가 발생했습니다.
분할 폴트 코드 섹션에서 제공 :
FOUT은 ofstream 객체가for(int i=0; i<listLength; i++)
fout << "Word " << i << (wordList[i])->getWord() << endl;
은 단어 목록은 ** 객체 워드이고 getWord()는 워드 오브젝트의 멤버 함수이다. 문제는 WordList의 다른 멤버 함수에서 동일한 wordList [i] -> getWord() 구문을 사용하고 적절한 출력을 얻는 것입니다.
더 많은 코드가 제대로 문제를 진단 할 필요가 있으면 알려 주시기 바랍니다
더 코드 :
는#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include "Word.h"
using namespace std;
class WordList
{
public:
int listLength_;
Word** wordList_;
WordList()
{
char blank = ' ';
char* blankPtr = ␣
setListLength(1);
wordList_ = new Word* [listLength_];
for(int i=0; i<listLength_; i++)
{
wordList_[i] = new Word(blankPtr);
}
}
void addWord(Word* word, Word** wordList, int n)
{
Word** wl_temp = new Word* [n+1];
for(int i=0; i<n; i++)
{
wl_temp[i] = wordList[i];
}
wl_temp[n] = word;
delete[] wordList;
setWordList(wl_temp);
listLength_++;
cout << " " << (wordList_[n]->getWord()); //works here
}
void parse(const char* filename)
{
ifstream fin(filename);
char end;
char* tw;
while(fin >> end)
{
fin.unget();
fin.get(tw=new char[49], 49, ' ');
Word* w = new Word(tw);
addWord(w, getWordList(), getListLength());
delete w;
delete[] tw;
}
}
void output(const char* outfile)
{
ofstream fout(outfile);
for(int i=1; i<=listLength_; i++)
fout << "Word " << i << (wordList_[i])->getWord() << endl; //not here
fout.close();
}
};
int main(int argc, char* argv[])
{
WordList wordList;
wordList.parse(argv[1]);
wordList.output(argv[2]);
return 1;
}
'wordList [i]'가 null이 아닌가요? 또한 배열과 그 멤버를 할당하는 방법을 보여주십시오. –
더 많은 코드를 제공해 주실 수 있습니까? 이상적으로 [sscce] (http://sscce.org/) – simonc
@JoachimPileborg 긍정적 인 wordList [i]는 다른 함수에서 액세스 할 수 있으므로 null이 아닙니다. –