如何使用Python从预定义的C ++函数中获取变量的值?

问题描述 投票:0回答:1

有没有办法使用Python从预定义的C ++函数中获取变量的值?我有C ++函数,它以variant类型为参数。但我们无法在python中定义variant类型变量,对吧?我尝试过使用Boost.Pythonpybind11。但由于我无法编辑预定义的函数,因此无法实现它们。

编辑:我有一个名为bcap的C ++函数,它接受2个参数并返回2个值。

# C++ code    
# Takes these arguments
int32 func_id
variant[] vntArgs
---
# Returns these values
int32 HRESULT
variant vntRet

variant[] vntArgs有两种类型的变量。

int16 vt
string value

我试图使用这些参数调用此函数。

#python code
    func_id = 3
    vt = 8
    value_1 = 'value: b-CAP'
    value_2 = 'CaoProv.DENSO.VRC'
    value_3 = 'localhost'
    value_4 = ''
    vntArgs = [vt, value_1, vt, value_2, vt, value_3, vt, value_4]

    response = bcap(func_id, vntArgs)

    #store the response in some variables
    result1 = response.HRESULT
    result2 = response.vntRet

问题是当我运行这个程序时,出现了这个错误。

AttributeError: 'int' object has no attribute 'vt'

我该怎么办?

提前致谢。

python c++ variant
1个回答
0
投票

您可以创建多个单独的重载,每个重载的可能组合一个。那会有用吗?

void actual_function(variant<int, double, string> v) {
    // stuff
}

void your_function(int i) {
    actual_function(i); 
}
void your_function(double d) {
    actual_function(d);
}
void your_function(string const& s) {
    actual_function(s); 
}
© www.soinside.com 2019 - 2024. All rights reserved.