我有以下代码:
//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 ++ boost libs进行编译)
文件physical_file.cpp和physical_file.h被编译为test.cpp使用的dll
答案 0 :(得分:3)
默认情况下,Boost.Python会自动为曝光类型注册to_python
次转换,例如Physical_file
。因此,Boost.Python要求这些类型是可复制的。在这种情况下,Physical_file
不可复制,其filefl
成员的类型不可复制:fstream
。
要解决此问题,请执行以下操作:
filefl
的复制/所有权语义,并通过持有者对象进行管理。例如,boost::shared_ptr
。在公开boost::noncopyable
类时,通过提供Physical_file
作为模板参数来禁止自动注册python转换。
BOOST_PYTHON_MODULE(python_bridge)
{
boost::python::class_<Physical_file, boost::noncopyable>("pf")
.def("pcreate", &Physical_file::pcreate)
;
}
有关将C ++类公开给Python时的更多选项,请参阅boost::python::class_
文档。