2010-03-15 2 views
0

내가 뭘 잘못하고 있는지 알 수 없습니다. 나는 그것이 3 가지 법칙 중 하나일지도 모른다고 생각한다.컨테이너가 분류되지 않음, 테스트 케이스 포함, (쉬운 질문?)

#include <deque> 
//#include <string> 
//#include <utility> 
//#include <cstdlib> 
#include <cstring> 
#include <iostream> 
//#include <algorithm> // I use sort(), so why does this still compile when commented out? 

#include <boost/filesystem.hpp> 
#include <boost/foreach.hpp> 

using namespace std; 

namespace fs = boost::filesystem; 

class Page 
{ 
    public: 
     // constructor 
     Page(const char* path, const char* data, int size) : 
      path_(fs::path(path)), 
      size_(size), 
      data_(new char[size]) 
     { 
//   cout << "Creating Page..." << endl; 
      strncpy(data_, data, size); 
//   cout << "done creating Page..." << endl; 
     } 
     // copy constructor 
     Page(const Page& other) : 
      path_(fs::path(other.path())), 
      size_(other.size()), 
      data_(new char[other.size()]) 
     { 
//   cout << "Copying Page..." << endl; 
      strncpy(data_, other.data(), size_); 
//   cout << "done copying Page..." << endl; 
     } 
     // destructor 
     ~Page() { delete[] data_; } 
     // accessors 
     const fs::path& path() const { return path_; } 
     const char* data() const { return data_; } 
     int size() const { return size_; } 
     // operators 
     Page& operator = (const Page& other) { 
      if (this == &other) 
       return *this; 
      char* newImage = new char[other.size()]; 
      strncpy(newImage, other.data(), other.size()); 
      delete[] data_; 
      data_ = newImage; 
      return *this; 
     } 
     bool operator < (const Page& other) const { return path_ < other.path(); } 
    private: 
     fs::path path_; 
     int size_; 
     char* data_; 
}; 

class Book 
{ 
    public: 
     Book(const char* path) : 
      path_(fs::path(path)) 
     { 
      cout << "Creating Book..." << endl; 
      cout << "pushing back #1" << endl; 
      pages_.push_back(Page("image1.jpg", "firstImage", 10)); 
      cout << "pushing back #3" << endl; 
      pages_.push_back(Page("image3.jpg", "thirdImage", 10)); 
      cout << "pushing back #2" << endl; 
      pages_.push_back(Page("image2.jpg", "secondImage", 11)); 

      cout << "testing operator <" << endl; 
      cout << pages_[0].path().string() << (pages_[0] < pages_[1]? " < " : " > ") << pages_[1].path().string() << endl; 
      cout << pages_[1].path().string() << (pages_[1] < pages_[2]? " < " : " > ") << pages_[2].path().string() << endl; 
      cout << pages_[0].path().string() << (pages_[0] < pages_[2]? " < " : " > ") << pages_[2].path().string() << endl; 

      cout << "sorting" << endl; 
      BOOST_FOREACH (Page p, pages_) 
       cout << p.path().string() << endl; 
      sort(pages_.begin(), pages_.end()); 
      cout << "done sorting\n"; 
      BOOST_FOREACH (Page p, pages_) 
       cout << p.path().string() << endl; 

      cout << "done Creating Book" << endl; 
     } 
    private: 
     deque<Page> pages_; 
     fs::path path_; 
}; 

int main() { 
    Book* book = new Book("/some/path/"); 
} 
+1

C 스타일 문자열의 널 종결 자에 여분의 바이트를 할당하지 않았을 수 있습니다. 따라서 문자열 비교가 실패 할 수 있습니다. – dirkgently

+0

비교가 경로에서 수행됩니다. C 스타일 문자열은 내 프로그램의 파일 스트림이고 정렬되지 않았기 때문에 "데이터"라는 이름이 붙었습니다. – Kache

+0

'op ='는'fs :: path' 객체도 업데이트하지 않습니다. – dirkgently

답변

0

Codepad link 난 그냥 장난 유지, 내 할당 연산자뿐만 아니라,뿐만 아니라 힙 할당 된 사람을 통해 다른 모든 매개 변수를 복사 할 필요가 있음을 깨달았다.

나는 멍청한 사람입니다. > _ <

Btw 추가 질문 : 모든 버퍼를 strncpy() 할 필요없이 정렬을 수행하고 포인터 주소를 대신 스왑하는 방법이 있습니까?

편집 : dirkgently

TNX. 그래, 그게 뭔지, sry이 게시하기 전에 귀하의 의견을 보지 못했어요.

+0

SO에 대한 별도의 질문으로 후속 질문을하는 것이 좋습니다. 그렇게 생각하는 사람이 더 많아지며 나중에 참조하기 쉽게 찾을 수 있습니다. –

+0

mmk, 조언 주셔서 감사합니다. – Kache