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++ ブースト ライブラリを使用してコンパイルしています)

ファイル 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)
        ;
    }
    

boost::python::class_C++ クラスを Python に公開する際のその他のオプションについては、ドキュメントを参照してください。

于 2013-06-10T17:26:59.320 に答える