次のように
iostreamオブジェクト(例えば、cout)をCythonでビルドして、それをC++関数に渡すには?私は、次のC++クラスをラップしようとしている
#include "print.h"
#include <iostream>
ControlParameters::ControlParameters()
{
}
ControlParameters::~ControlParameters()
{
}
void ControlParameters::Initialize(string input, ostream& log)
{
log<<"Output = "<< input <<endl;
}
PRINT.H
#include <string>
using namespace std;
class ControlParameters
{
public:
ControlParameters();
~ControlParameters();
void Initialize(string input, ostream& log);
};
print.cppは私のString.pyxに見えます:
from libcpp.string cimport string
cdef extern from "<iostream>" namespace "std":
cdef cppclass ostream:
┆ ostream& write(const char*, int) except +
cdef extern from "print.h":
cdef cppclass ControlParameters:
┆ ControlParameters() except +
┆ void Initialize(string, ostream&)
cdef class PyControlParameters:
cdef ControlParameters *thisptr
def __cinit__(self):
┆ self.thisptr = new ControlParameters()
def __dealloc__(self):
┆ del self.thisptr
def PyInitialize(self, a,b):
self.thisptr.Initialize(a, b)
String.pyをコンパイルした後、次のエラーが表示されます。
Compiling String.pyx because it changed.
[1/1] Cythonizing String.pyx
Error compiling Cython file:
------------------------------------------------------------
...
def __cinit__(self):
self.thisptr = new ControlParameters()
def __dealloc__(self):
del self.thisptr
def PyInitialize(self, a,b):
self.thisptr.Initialize(a, b)
^
------------------------------------------------------------
String.pyx:18:39: Cannot convert Python object to 'ostream'
Traceback (most recent call last):
File "setup.py", line 7, in <module>
language="c++"
File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 1039, in cythonize
cythonize_one(*args)
File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 1161, in cythonize_one
raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: String.pyx
here私は自分自身のostreamクラスを構築してostreamオブジェクトを取得する必要があると読んでいます。しかし、私はこのオブジェクトをどのように定義するのですか? 私は自分のpythonオブジェクトをostreamのオブジェクトに変換する関数が必要だと思います。どのように私はそのような機能を実装することができますか?
出力で実際に何をしたいですか?それをファイルに送信しますか?それを標準に送りますか? 2つの間で選択肢がありますか? – DavidW
@ DavidW私は標準に送信したいです。 C + +では、私は叫んで渡すだろう。 –