Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C++; 我试图从C++调用一个类方法。我试过所有的组合 rb_实习生我可以想办法让它工作,但我什么都没有得到_C++_Ruby - Fatal编程技术网

从C++; 我试图从C++调用一个类方法。我试过所有的组合 rb_实习生我可以想办法让它工作,但我什么都没有得到

从C++; 我试图从C++调用一个类方法。我试过所有的组合 rb_实习生我可以想办法让它工作,但我什么都没有得到,c++,ruby,C++,Ruby,示例类 class CallTest def go (do something here) end end 类调用测试 def go (在这里做点什么) 结束 结束 尝试在C++中调用: rb_funcall(?, rb_intern("go"), 0); rb_funcall(?,rb_实习生(“go”),0); 盒子里装的是什么?空间我知道如果我在那里使用Qnil,它会调用 全局函数,但我更喜欢类方法 我走错方向了吗 另外,如果需要的话,我不想提前知道类名 可能,但如果我

示例类

class CallTest def go (do something here) end end 类调用测试 def go (在这里做点什么) 结束 结束 尝试在C++中调用:

rb_funcall(?, rb_intern("go"), 0); rb_funcall(?,rb_实习生(“go”),0); 盒子里装的是什么?空间我知道如果我在那里使用Qnil,它会调用 全局函数,但我更喜欢类方法

我走错方向了吗

另外,如果需要的话,我不想提前知道类名 可能,但如果我必须要求我知道它是什么,我可以试试 通过名称将其传递给我的应用程序


我正在使用SWIG生成绑定。

首先,
go
不是一个类方法,而是一个实例方法

作为一种面向对象的语言,所有ruby方法都需要一个接收者,即调用该方法的对象。对于实例方法,接收方是类的实例,对于类方法,接收方是类对象本身

那个?您拥有的占位符是方法调用接收器的插槽

如果希望将其保留为实例方法,则需要执行以下操作:

rb_funcall(a_CallTest_instance, rb_intern("go"), 0);
其中,
a\u CallTest\u实例
是您使用
rb\u class\u new\u instance()
创建的CallTest实例

如果将其设置为类方法:

class CallTest
  def self.go
    # ...
  end
end
然后您需要使用
CallTest
类本身作为接收器:

rb_funcall(klass, rb_intern("go"), 0);
您可以使用
rb\u const\u get()

在那里使用
rb_cObject
,因为
CallTest
是在全局上下文中定义的


我建议阅读鹤嘴锄中关于扩展ruby的章节。

我也使用SWIG。这些特定的示例文件应该对您有所帮助

1) test.rb

2) 测试h:

输出:

ruby ./test.rb 
ruby instance method: Hello
ruby global method: Yabaaa
ruby module method: Yahooooooo

如果我在Ruby文件中创建了一个类,我现在正在运行,那么如何将它传递回C++程序以有效地访问?您想如何调用Ruby的C++代码?您可以将C++代码作为该实例的方法,或者作为其他方法的一种方法,并将实例传递为参数。无论哪种方式,都需要使用<代码> RBYRealEngEngy()/<代码>来将C++函数变成一种方法。如果这是一个顶级问题,我很乐意(并且能够,不受字符限制)更全面地回答。非常感谢,我一直在努力找出如何按名称查找类或模块。
require 'test.so'
class A
    def func1(buffer)
        puts "ruby instance method: #{buffer}"
    end
end
def func2(buffer)
    puts "ruby global method: #{buffer}"
end
module Z
    def Z.func3(buffer)
        puts "ruby module method: #{buffer}"
    end
end

a = A.new
t = Test::Test1.new()
t.call(a, "func1", "Hello", 5)
t.call(method(:func2), "func2", "Yabaaa", 6)
t.call(Z, "func3", "Yahooooooo", 10)
#include <ruby.h>
class Test1
{
public:
    void call(VALUE obj, char* func_name, const char* buffer, int size)
    {
        VALUE val = rb_str_new(buffer, size);   
        rb_funcall(obj, rb_intern(func_name), 1, val);
    }
};
%module test

%{
#include "test.h"
%}

%exception
{
    try
    {
        $action
    }
    catch (std::exception& ex)
    {
        static VALUE cpperror = rb_define_class("test Exception", rb_eStandardError);
        rb_raise(cpperror, ex.what());
    }
    catch (...)
    {
        static VALUE cpperror = rb_define_class("test UnknownException", rb_eStandardError);
        rb_raise(cpperror, "Unknown catched");
    }
}

%include "test.h"
ruby ./test.rb 
ruby instance method: Hello
ruby global method: Yabaaa
ruby module method: Yahooooooo