2

我有以下代码:

//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 库进行编译)

文件 physical_file.cpp 和 physical_file.h 被编译为 test.cpp 使用的 dll

4

1 回答 1

3

默认情况下,Boost.Python 会自动注册to_python公开类型的转换,例如Physical_file. 因此,Boost.Python 要求这些类型是可复制的。在这种情况下,Physical_file是不可复制的,它的filefl成员是不可复制的类型:fstream.

要解决此问题,请执行以下任一操作:

  • 确定 的复制/所有权语义filefl,并通过持有者对象对其进行管理。例如,boost::shared_ptr
  • 通过在公开类boost::noncopyable时提供作为模板参数来抑制 python 转换的自动注册。Physical_file

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

有关将 C++ 类公开给 Python 的更多选项,请参阅boost::python::class_文档。

于 2013-06-10T17:26:59.320 回答