2013-06-09 1 views
2

나는 다음과 같은 코드를 가지고 : I를 컴파일 할 때부스트 파이썬에 액세스 할 수 없습니다 IOS 개인 항목

//test.cpp 
#include <Physical_file.h> 
#include <boost\python.hpp> 

using namespace boost::python; 
using namespace FMS_Physical; 


BOOST_PYTHON_MODULE(python_bridge) 
{ 
    class_<Physical_file>("pf") 
     .def("pcreate", &Physical_file::pcreate) 
    ; 
} 

//physical_file.h 
#include <fstream> 
#include "tools.h" 
using namespace std; 

#define FMS_lvl0_DLL_API __declspec(dllexport) 

namespace FMS_Physical 
{ 
    const int BLOCK_OFFSET = 20 + 4; 
    const string SUFFIX(".hash"); 

    struct block 
    { 
     unsigned int BlockNr; 
     char filler[20]; 
     char data[1024 - BLOCK_OFFSET]; 
    }; 

    class Physical_file 
    { 
    public: 
     fstream filefl; 
     string workingDir; 
     string fileName; 
     int fileSize; 
     block currBlock; 
     block FHBuffer; 
     bool opened; 
     string openMode; 

     /************ 
     functions 
     ************/ 

     FMS_lvl0_DLL_API Physical_file(void); 
     FMS_lvl0_DLL_API Physical_file(string &FileName, int FileSize, string &Dir = getCurrentPath()); 
     FMS_lvl0_DLL_API Physical_file(string &FileName, string &Type, string &Dir = getCurrentPath()); 

     FMS_lvl0_DLL_API ~Physical_file(void); 

     void FMS_lvl0_DLL_API pcreate(string &Name, int Size = 1000, string &Dir = getCurrentPath()); 
     void FMS_lvl0_DLL_API pdelete(void); 
     void FMS_lvl0_DLL_API popen(string &name, string &OpenMode = string("I"), string &Dir = getCurrentPath()); 
     void FMS_lvl0_DLL_API pclose(void); 

     void FMS_lvl0_DLL_API seekToBlock(unsigned int BlockNr); 

     void FMS_lvl0_DLL_API WriteBlock(void); 
     void FMS_lvl0_DLL_API ReadBlock(void); 
     void FMS_lvl0_DLL_API WriteFH(void); 
     void FMS_lvl0_DLL_API ReadFH(void); 

    }; 
} 

//physical_file.cpp 
void Physical_file::pcreate(string &Name, int Size, string &Dir) 
{ 
    if (Dir.compare("") == 0) 
     Dir = getCurrentPath(); 
    string fileFullName = Dir + '\\' + Name + SUFFIX; 
    this->filefl.open(fileFullName.c_str(),ios::in | ios::binary); 
    if (filefl.is_open()) 
    { 
     throw new exception((string("in function Physical_file::pcreate, file:") + fileFullName + " exists.").c_str()); 
    } 
    try{ 
     this->filefl.open(fileFullName.c_str(),ios::binary | ios::out); 
     this->opened = true; 
     this->seekToBlock(0); 
     this->currBlock.BlockNr = 0; 
     for (int i = 0; i < Size; i++) 
     { 
      for (int j = 0; j < sizeof(currBlock.data); j++) 
       this->currBlock.data[j] = 0; 
      for (int j = 0; j < sizeof(currBlock.filler); j++) 
       this->currBlock.filler[j] = 0; 
      this->WriteBlock(); 
     } 

     this->pclose(); 

     this->fileName = Name; 
     this->workingDir = Dir; 
     this->fileSize = Size; 
    } 
    catch(exception e) 
    { 
     throw new exception("in Physical_file::pcreate \n" + *e.what()); 
    } 
} 
Physical_file::Physical_file(void) 
{ 
    this->fileName = string(""); 
    this->workingDir = string(""); 
    this->opened = false; 
} 

가 (나는 문제의 관련성을 좀 더 코드가 생각됩니다)

을 다음 오류가 발생합니다.

error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>' 

누구든지이 문제가 발생하는 이유와 해결 방법을 설명 할 수 있습니까?

은 (내가 VS2010, python27을 사용하여 컴파일하고 있습니다와 c를 libs와 향상 ++)

파일의 physical_file.cpp 및 physical_file.h는 Test.cpp에 기본적으로

+0

'std :: string ("")'을 사용하지 마십시오. 그냥'std :: string()'을 사용하십시오. 기본 생성자는 빈 리터럴 (GCC/libstdC++ 이상)에서 초기화하는 것보다 훨씬 효율적입니다. –

답변

3

, 부스트 사용하는 DLL로 컴파일 . 파이썬은 노출 된 유형에 대해 to_python 개의 전환을 자동으로 등록합니다 (예 : Physical_file). Boost.Python은 이러한 유형을 복사 할 수 있어야합니다. 이 경우 Physical_file은 복사 할 수 없습니다 filefl 회원은 복사 할 수없는 유형입니다 : fstream.

  • filefl의 복사/소유권의 의미를 결정하고 홀더 객체를 통해 관리 :

    중 하나,이 문제를 해결하려면. 예 : boost::shared_ptr.
  • Physical_file 클래스를 노출 할 때 boost::noncopyable을 템플릿 인수로 제공하여 파이썬 변환 자동 등록을 억제합니다.

    BOOST_PYTHON_MODULE(python_bridge) 
    { 
        boost::python::class_<Physical_file, boost::noncopyable>("pf") 
        .def("pcreate", &Physical_file::pcreate) 
        ; 
    } 
    
  • 추가 옵션을 보려면

파이썬에 C++ 클래스를 노출

boost::python::class_ 설명서를 참조하십시오.

+0

두 번째 솔루션을 시도하고 코드가 컴파일되었습니다. 파이썬에서 dll 가져 오기도 잘 작동하지만 'pf'는 정의되지 않습니다. 어떤 생각이야? – elyashiv

+0

라이브러리 이름이'BOOST_PYTHON_MODULE'에 제공된 모듈 이름과 일치하는지 확인하십시오. 또한,'import'가 당신이 기대하는 모듈 ('PYTHONPATH' 등이 아닌 이름 충돌)을로드하고 있는지 확인하십시오. [-vv] (http://docs.python.org/3.1/using/cmdline.html#cmdoption-trace-v) 옵션은로드되는 내용에 대한 더 자세한 정보를 제공합니다. –

관련 문제