[英]How can I pass an object I wrote into a constructor of another class?
我正在实现一个 Duplicator 类,它允许我复制游戏对象。 我需要能够创建一个与我拥有的相同的 Game 对象。 这存在于具有其他几个类(例如 Board 和 Space)的棋盘游戏的更大实现中。
我有两个用于 Duplicator 类的文件:
复制器.h
#ifndef DUPLICATOR_H
#define DUPLICATOR_H
#include <Rcpp.h>
#include <stack>
#include "Game.h"
using namespace Rcpp;
class Duplicator {
private:
Game gameObj = Game(5);
public:
Duplicator(Game g);
// Game genDuplicate();
};
#endif
复制器.cpp
#include <Rcpp.h>
#include <vector>
#include "Game.h"
#include "Duplicator.h"
using namespace Rcpp;
Duplicator::Duplicator(Game g){
gameObj = g;
}
RCPP_EXPOSED_CLASS(Duplicator)
RCPP_MODULE(duplicator_cpp) {
class_<Duplicator>("Duplicator")
.constructor<Game>()
;
我不断收到的错误是:
没有用于初始化“游戏”的匹配构造函数
游戏类包含在两个文件中。
游戏.h
#ifndef GAME_H
#define GAME_H
#include <Rcpp.h>
using namespace Rcpp;
class Game {
private:
int id;
public:
Game(int n);
};
#endif
游戏.cpp
#include <Rcpp.h>
#include "Game.h"
using namespace Rcpp;
Game::Game(int n){
id = n;
}
RCPP_EXPOSED_CLASS(Game)
RCPP_MODULE(game_cpp) {
class_<Game>("Game")
.constructor<int>()
;
}
我不太确定我需要做什么。 似乎我需要在 Duplicator 类中为 Game 提供一个构造函数。
您必须将RCPP_EXPOSED_CLASS(...)
移动RCPP_EXPOSED_CLASS(...)
文件中,至少当您想将该类用作其他编译单元中的参数或返回类型时。 否则编译器不知道例如Game
可以转换为SEXP
,反之亦然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.