使用Boost Python为C++类用户输入 我有一个C++类,它在构造函数中输入用户输入,然后把它和其他东西写入文件。在MSVC和GCC上,它在C++上运行非常完美,现在我想在Python项目中使用这个类。我的档案是:
福安 如有任何想法,请遵照执行,我们将不胜感激使用Boost Python为C++类用户输入 我有一个C++类,它在构造函数中输入用户输入,然后把它和其他东西写入文件。在MSVC和GCC上,它在C++上运行非常完美,现在我想在Python项目中使用这个类。我的档案是:,c++,visual-studio-2012,gcc,boost,boost-python,C++,Visual Studio 2012,Gcc,Boost,Boost Python,福安 如有任何想法,请遵照执行,我们将不胜感激 请提前感谢。代码中的一个明显错误是,Foo的构造函数使用了两个您没有包含在包装器中的参数: // Boost.Python wrapper BOOST_PYTHON_MODULE(foo) { boost::python::class_<Foo, boost::noncopyable>("Foo", boost::python::init<const std::string, const std::strin
请提前感谢。代码中的一个明显错误是,Foo的构造函数使用了两个您没有包含在包装器中的参数:
// Boost.Python wrapper
BOOST_PYTHON_MODULE(foo)
{
boost::python::class_<Foo, boost::noncopyable>("Foo",
boost::python::init<const std::string, const std::string>())
.def("Write", &Foo::Write)
;
}
这就解释了第二个错误,这个不可复制的版本现在应该可以编译了。这非常有效!然而,现在我在尝试构建python包装库时遇到了奇怪的错误。我在这里问了一个不同但相关的问题。如果你能看一看,那就太棒了。谢谢
#include <Python.h>
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
// Constructor
Foo::Foo(const std::string file_name, const std::string other_input)
{
std::ifstream file_exists(file_name)
if(file_exists.good())
output_file.open(file_name, std::ios_base::app);
else
output_file.open(file_name);
random_string = other_input;
}
// Destructor
Foo::~Foo()
{
output_file.close();
}
// Write to a file
void Foo::Write(const std::string random_text)
{
sprintf( buffer, "%s", random_string );
output_file << buffer << ";\n";
}
// Boost.Python wrapper
BOOST_PYTHON_MODULE(foo)
{
boost::python::class_<Foo>("Foo", boost::python::init<>())
.def("Write", &Foo::Write)
;
}
'std::basic_ofstream<_Elem,_Traits>::basic_ofstream' : cannot access private member declared in class 'std::basic_ofstream<_Elem,_Traits>'
// Boost.Python wrapper
BOOST_PYTHON_MODULE(foo)
{
boost::python::class_<Foo, boost::noncopyable>("Foo", boost::python::init<>())
.def("Write", &Foo::Write)
;
}
'Foo' : no appropriate default constructor available
// Boost.Python wrapper
BOOST_PYTHON_MODULE(foo)
{
boost::python::class_<Foo, boost::noncopyable>("Foo",
boost::python::init<const std::string, const std::string>())
.def("Write", &Foo::Write)
;
}