我阅读了有关Rcpp中的类和模块的博客文章,并尝试重新创建它,但是遇到了麻烦。
这是帖子中代码的缩写版:
#include <Rcpp.h>
using namespace Rcpp;
class Point {
public:
Point( double x_, double y_) : x(x_), y(y_){}
double x, y ;
} ;
class Shape {
public:
Shape( const Point& center_ ) : center(center_){}
Point center ;
virtual double area() const { return 0.0 ;}
virtual bool contains(const Point& point) const { return false ; }
} ;
RCPP_MODULE(play){
class_<Point>("Point")
.constructor<double,double>()
.field( "x", &Point::x)
.field( "y", &Point::y)
;
class_<Shape>( "Shape" )
.constructor<Point>()
.method( "area", &Shape::area )
.method( "contains", &Shape::contains )
;
};
这里发生的所有事情都是创建Point类,然后将其用作Shape类的构造函数中的参数。但是Shape类不会接受Point类作为构造函数中的参数。编译上面的代码时,出现错误:没有匹配的函数可调用“ Point :: Point(SEXPREC *&)”。我相信这个错误是在说Shape的构造函数不了解如何处理Point类。
我已经阅读了SpringerRcpp
教科书中有关模块的章节,并且阅读了有关模块的小插图,但是我不相信其中任何一个示例都具有从其他类(如上述博客文章)构造类的示例。我一定是误会了一些东西,所以如果有人能启发我,我将不胜感激。
问候
我不知道究竟为什么要在这个博客帖子的代码是(大概)有效,当它被写,不再编制,但在三年前,这是大概,从那时起一些变化RCPP的模块组件已经发生。要使其编译,可以在类定义之前添加以下内容:
class Point; // fwd declarations
class Shape;
class Circle;
class Rectangle;
RCPP_EXPOSED_CLASS(Point);
RCPP_EXPOSED_CLASS(Shape);
RCPP_EXPOSED_CLASS(Circle);
RCPP_EXPOSED_CLASS(Rectangle);
我从Romain的C ++示例中获取了完整的代码(请参见下文,进行了一些小的修改),只是为了能够运行博客文章中也包含的R代码是很方便的:
origin <- new( Point, 0, 0 )
pie <- new( Circle, origin, 3 )
##
R> pie$area()
#[1] 28.27433
R> pie$contains( new( Point, 1, 2 ) )
#[1] TRUE
##
rec <- new( Rectangle, origin, 2, 3 )
R> rec$area()
#[1] 6
R> rec$contains( new( Point, 1, 2 ) )
#[1] FALSE
完整代码(您的编译器virtual
在中没有析构函数时会抱怨Shape
):
#include <Rcpp.h>
class Point; // fwd declarations
class Shape;
class Circle;
class Rectangle;
RCPP_EXPOSED_CLASS(Point);
RCPP_EXPOSED_CLASS(Shape);
RCPP_EXPOSED_CLASS(Circle);
RCPP_EXPOSED_CLASS(Rectangle);
class Point {
public:
Point( double x_, double y_) : x(x_), y(y_){}
double x, y ;
} ;
double square( double x) {
return x*x ;
}
double distance( const Point& p1, const Point& p2 ){
return sqrt( square( p1.x - p2.x) + square( p1.y - p2.y ) ) ;
}
class Shape {
public:
Shape( const Point& center_ ) : center(center_){}
virtual ~Shape() {}
Point center ;
virtual double area() const { return 0.0 ;}
virtual bool contains(const Point& point) const { return false ; }
} ;
class Circle : public Shape {
public:
Circle( Point center_, double radius_ ): Shape(center_), radius(radius_){}
double area() const {
return PI * square( radius ) ;
}
bool contains( const Point& point ) const {
return distance(point, center) < radius ;
}
double radius ;
} ;
class Rectangle : public Shape {
public:
Rectangle( Point center_, double width_, double height_ ) :
Shape(center_), width(width_), height(height_){}
double area() const {
return width * height ;
}
bool contains( const Point& point ){
return (point.x >= ( center.x - width / 2.0 )) &&
(point.x <= ( center.x + width / 2.0 )) &&
(point.y >= ( center.y - height / 2.0 )) &&
(point.y <= ( center.y + height / 2.0 ));
}
double width, height ;
} ;
RCPP_MODULE(play){
using namespace Rcpp;
class_<Point>("Point")
.constructor<double,double>()
.field( "x", &Point::x)
.field( "y", &Point::y)
;
class_<Shape>( "Shape" )
.constructor<Point>()
.method( "area", &Shape::area )
.method( "contains", &Shape::contains )
;
class_<Circle>( "Circle" )
.derives<Shape>("Shape" )
.constructor<Point,double>()
.field( "r", &Circle::radius )
;
class_<Rectangle>( "Rectangle" )
.derives<Shape>("Shape" )
.constructor<Point,double,double>()
.field( "h", &Rectangle::height )
.field( "w", &Rectangle::width )
;
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句