我有一个非常简单的测试用例,我无法工作,我正在尝试使用ctypes将c ++与python接口.我在使用双打时遇到错误,在这种情况下尝试在c ++中使用"cout".
错误是:
WindowsError: exception: access violation writing 0x.....
Run Code Online (Sandbox Code Playgroud)
问题在于以下c ++代码的cout行:
#include "testgeo.h"
#include <iostream>
TestGeo::TestGeo() : td_(0),
ti_(0) {
std::cout<<td_<<std::endl; // problem line
}
Run Code Online (Sandbox Code Playgroud)
其中包含以下标题(testgeo.h),包括extern C部分:
class TestGeo {
public:
TestGeo();
~TestGeo(){};
private:
double td_;
int ti_;
};
extern "C" {
__declspec(dllexport) TestGeo* TestGeo_new() {
return new TestGeo();
}
}
Run Code Online (Sandbox Code Playgroud)
运行它的python代码是(testgeo.py):
import ctypes
lib = ctypes.cdll.LoadLibrary('testgeo.dll')
class TestGeo(object):
lib.TestGeo_new.argtypes = []
lib.TestGeo_new.restype = ctypes.c_void_p
def __init__(self):
self.obj = lib.TestGeo_new()
if __name__ == "__main__":
testGeoObj = TestGeo()
Run Code Online (Sandbox Code Playgroud)
编辑1:仍在努力,我对编程很陌生.无论如何我可以进一步调查内存错误,这可能会给我一些线索吗?
编辑2:我想我会分享我如何编译,以防出现错误: …
我将称之为“所有者”的一个对象在其生命周期内拥有数据对象向量的明确所有权。
这些存储为 unique_ptr 的向量。
一个对象/类,称为“输出”,需要以许多不同的方法查看这些数据对象,因此某种引用/指针/变量是“输出”的成员变量。
输出在其构造函数中接收数据对象的向量。
我已经想到了三种方法来实现这一目标。什么会被认为是最好的方法?
选项 1 - “输出”对象将数据 vec 存储为常量引用:
class Output {
// output wants the data:
public:
Output(std::vector<std::unique_ptr<Data>> const & in)
: my_lot_of_data(in) {
};
std::vector<std::unique_ptr<Data>> const & my_lot_of_data;
}
Run Code Online (Sandbox Code Playgroud)
它由“所有者”实例化:
data_vec_.push_back(std::unique_ptr<Data>(new Data));
/* stuff happens to data */
Output output(data_vec_);
Run Code Online (Sandbox Code Playgroud)
选项 2 - “输出”对象将数据 vec 存储为常量指针:
class Output {
// output wants the data:
public:
Output(std::vector<std::unique_ptr<Data>> const * in)
: my_lot_of_data(in) {
};
std::vector<std::unique_ptr<Data>> const * my_lot_of_data;
}
Run Code Online (Sandbox Code Playgroud)
它由“所有者”实例化:
data_vec_.push_back(std::unique_ptr<Data>(new Data));
/* …Run Code Online (Sandbox Code Playgroud) 我尝试使用Eigen库来创建样条曲线.但是,一旦我创建样条曲线,我就不知道如何得到给定点x的值.
请参阅下面的示例,并解释我的意图:
#include <Eigen/Core>
#include <unsupported/Eigen/Splines>
int main(int argc, char const* argv[])
{
// points at (0,0) (15,12) and (30,17)
Eigen::MatrixXd points(2, 3);
points << 0, 15, 30,
0, 12, 17;
typedef Eigen::Spline<double, 2> spline2d;
spline2d s = Eigen::SplineFitting<spline2d>::Interpolate(points, 2);
// I now have a spline called s.
// I want to do something like:
double x = 12.34;
double new_y = s(x)[1]; // However this s() function uses a chord value. What is a chord value?
// Is there …Run Code Online (Sandbox Code Playgroud) 我有一个困扰我的问题,我的c ++理解中缺少某些东西.
我正在尝试用boost :: filesystem :: path构建一个对象(它本身是从basic_string构造的)我以为我可以通过在构造同一行的构建boost路径来保存一行代码我的目标.
请参阅以下示例:
我不明白的一件事是,虽然它不起作用,但我也没有错.即似乎没有任何事情发生.那为什么会这样?
class Test {
public:
Test(boost::filesystem::path in) {
std::cout << "Succesful construction" << std::endl;
}
};
int main() {
std::string str("asdf.txt");
boost::filesystem::path p(str);
Test test1(boost::filesystem::path(str)); // Nothing at all happens, but no error
Test test2(p); // "Succesful construction"
}
Run Code Online (Sandbox Code Playgroud)