2013-02-28 3 views
0

아래에 정의 된 gml 파일에 쓰는 클래스가 있습니다. 이 수업에는 작문을하는 한 가지 방법이 있습니다. 함수를 호출하면 주 함수가 반환 될 때 코어 덤프가 발생합니다. 문제없이 클래스의 객체를 만들 수 있습니다. 쓰기 함수가 호출 될 때만 발생합니다. 이 함수는 오류없이 반환되고 나머지 프로그램이 실행됩니다.main 함수가 돌아 오는 코어 덤프

GML 글쓴이 :

class GMLWriter { 
public: 
    void write(List<User*> usr, const char* filename); 
}; 

void GMLWriter::write(List<User*> usr, const char* filename) 
{ 
    cout << "Filename: " << filename << endl; 
    ofstream outfile; 
    outfile.open(filename); 
    if (!outfile.is_open()) 
     cout << "Couldn't open the file..." << endl; 
    outfile << "graph [\n"; 

    // Write user data 
    for (int n = 0; n < usr.size(); n++) { 
     cout << "Writing node..." << endl; 
     outfile << "node [\n"; 
     outfile << "id " << usr[n]->getID() << "\n"; 
     outfile << "name \"" << usr[n]->getName() << "\"\n"; 
     outfile << "age " << usr[n]->getAge() << "\n"; 
     outfile << "zip " << usr[n]->getZip() << "\n"; 
     outfile << "]\n"; 
    } 

    // Write associations 
    for (int n = 0; n < usr.size(); n++) { 
     List<int> tList = usr[n]->getFriends(); 
     cout << "Writing edge..." << endl; 
     //List<int> tempL = usr[n]->getFriends(); 
     for (int i = 0; i < tList.size(); i++) { 
      outfile << "edge [\n"; 
      outfile << "source " << usr[n]->getID() << "\n"; 
      outfile << "target " << tList[i] << "\n"; 
      outfile << "]\n"; 
     } 
    } 

    outfile << "]"; // end graph 
    cout << "End function" << endl; 
    outfile.close(); 
} 

사용자 단순히 파일에 쓸 변수를 포함하고, 그 방법은 잘 작동합니다.

디버거에서 몇 시간을 보냈지 만 문제를 찾을 수 없었습니다. 어떤 도움이라도 대단히 감사하겠습니다.

감사합니다.

+0

당신이 당신의 전화 라인에서 소스를 포함시겠습니까? – L0j1k

+0

출력을 표시 할 수 있습니까? "Writing Node .."또는 "Writing Edge"가 보입니까? –

+0

클래스'List '에 * 소멸자 *를 게시하십시오. 그런 다음 매개 변수를 작성자로 변경하여 복사본 대신 const 참조를 가져옵니다. 마지막으로 [** The Rule of Three **] (http://en.wikipedia.org/wiki/Rule_of_three_ (C % 2B % 2B_programming))를 따르십시오. List :: ~ List()'는 목록의 내용을 삭제한다고 생각하고 있습니다. 즉, 값으로 전달한리스트가 빠져 나올 때 그 포인터를 지우고 복사 생성자를 제공하지 않았을 가능성이 있기 때문입니다. 'List '클래스에서 기본값이 사용되었습니다. 따라서'main()'에서 우리는 이제'List '에 삭제 된 포인터로 가득 찬 목록을 가지고 있습니다. 메인을 떠나고 붐을 일으키십시오. – WhozCraig

답변