为什么" RED1.Status(0)"不适用于" test.py"?

时间:2017-12-06 00:25:32

标签: c++ python-2.7 class

为什么" RED1.Status(0)"不适用于" test.py"?

我得到的输出是:

name:RED 1  color:RED  status:0 

name:RED 1  color:RED  status:1 

错误讯息:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<string>", line 3, in <module>
  NameError: name 'RED1' is not defined

我的代码:

#include <stdio.h>
#include <iostream>
using namespace std;

#include <string.h>
#include <python2.7/Python.h>

class LED{
  public:
    string name;
    string color;

    int Status(int st){
      if (st == 0) printf("name:%s  color:%s  status:%d \n", name.c_str(), color.c_str(), 0);
      if (st == 1) printf("name:%s  color:%s  status:%d \n", name.c_str(), color.c_str(), 1);
      return (0);
    }                             
};

int main(int argc, char *argv[]){
  LED RED1;
  RED1.name = "RED 1";
  RED1.color = "RED";

  RED1.Status(0);
  RED1.Status(1);

  Py_Initialize();

  PyRun_SimpleString("exec(open('test.py').read())");

  Py_Finalize();    
  return 0;
}

*

//test.py
#!/usr/bin/python
print "Python"
RED1.Status(0)

0 个答案:

没有答案