[英]How can I pass an object I wrote into a constructor of another class?
I am implementing a Duplicator class that will allow me to duplicate Game objects.我正在实现一个 Duplicator 类,它允许我复制游戏对象。 I need to be able to create an identical Game object to the one I have.
我需要能够创建一个与我拥有的相同的 Game 对象。 This is exists within a larger implementation of a board game with several other classes such as Board and Space.
这存在于具有其他几个类(例如 Board 和 Space)的棋盘游戏的更大实现中。
I have two files for the Duplicator class:我有两个用于 Duplicator 类的文件:
Duplicator.h复制器.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
Duplicator.cpp复制器.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>()
;
The error I keep getting is:我不断收到的错误是:
no matching constructor for initialization of 'Game'
没有用于初始化“游戏”的匹配构造函数
The game class is contained in two files.游戏类包含在两个文件中。
Game.h游戏.h
#ifndef GAME_H
#define GAME_H
#include <Rcpp.h>
using namespace Rcpp;
class Game {
private:
int id;
public:
Game(int n);
};
#endif
Game.cpp游戏.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>()
;
}
I'm not quite sure what I need to do.我不太确定我需要做什么。 It seems like I need to supply a constructor for Game in the Duplicator class.
似乎我需要在 Duplicator 类中为 Game 提供一个构造函数。
You have to move RCPP_EXPOSED_CLASS(...)
to the header file, at least when you want to use that class as an argument or return type in other compilation units.您必须将
RCPP_EXPOSED_CLASS(...)
移动RCPP_EXPOSED_CLASS(...)
文件中,至少当您想将该类用作其他编译单元中的参数或返回类型时。 Otherwise the compiler does not know that eg Game
can be converted to a SEXP
and vice versa.否则编译器不知道例如
Game
可以转换为SEXP
,反之亦然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.