我有3个不同的对象A,B和C.根据给定的参数,我想在这些不同的对象中进行选择.在编程中,
class A {
public:
void printHello() { cout << "HELLO A" << endl; }
};
class B {
public:
void printHello() { cout << "HELLO B" << endl; }
};
class C {
public:
void printHello() { cout << "HELLO C" << endl; }
};
int main () {
string key = "c";
A a;
B b;
C c;
Object obj; // I don't know how to declare Object.
if (key == "a") …Run Code Online (Sandbox Code Playgroud) 我有一个A像这样的python类.
class A:
def __init__(self, name):
self.name = name
def print_lastname(self, lastname):
print(lastname)
Run Code Online (Sandbox Code Playgroud)
我必须像这样调用这个代码.
import B
a = B.A("hello")
a.print_lastname("John")
Run Code Online (Sandbox Code Playgroud)
目前,我需要A从我的C++代码中使用这个类.我到目前为止.
Py_Initialize();
string hello = "hello";
PyObject *module, *attr, *arg;
module = PyObject_ImportModule("B"); // import B
attr = PyObject_GetAttrString(module, "A"); // get A from B
arg = PyString_FromString(hello.c_str());
instance = PyInstance_New(attr, arg, NULL); // trying to get instance of A with parameter "hello"
Py_Finalize();
Run Code Online (Sandbox Code Playgroud)
但我收到了错误
异常TypeError:来自'/usr/lib64/python2.7/threading.pyc'的模块'threading'中的'参数列表必须是元组'
如何从C import语句到a.print_name("John")C++实现?任何帮助表示赞赏.
我正在尝试使用模板动态设置struct中的字段.我在代码中写了两种方法.这两种方法都不起作用说no member named t.age.我怎么能动态设置字段?任何帮助表示赞赏.
#include <iostream>
using namespace std;
struct hello {
string name;
};
struct bye {
int age;
};
template <typename T>
void printHello(string key) {
T t;
if (key == "HELLO") {
t.name = "John";
}
else {
t.age = 0;
}
}
template <typename T>
T setStruct(T t) {
if (typeid(t) == typeid(hello)) {
t.name = "John";
}
else {
t.age = 0;
}
return t;
}
int main() {
//printHello<hello>("HELLO"); // ERROR: no …Run Code Online (Sandbox Code Playgroud)