如何将我写入的对象传递给另一个 class 的构造函数?
How can I pass an object I wrote into a constructor of another class?
我正在实现一个复制器 class,它将允许我复制游戏对象。我需要能够创建一个与我拥有的相同的游戏对象。这存在于棋盘游戏的更大实现中,其中包含其他几个 classes,例如 Board 和 Space。
我有两个复印机文件class:
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
Duplicator.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>()
;
我不断收到的错误是:
no matching constructor for initialization of 'Game'
游戏 class 包含在两个文件中。
Game.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
#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 class.
中为 Game 提供一个构造函数
您必须将 RCPP_EXPOSED_CLASS(...)
移至头文件,至少当您想将 class 用作参数或在其他编译单元中输入 return 时。否则编译器不知道,例如Game
可以转换为 SEXP
,反之亦然。
我正在实现一个复制器 class,它将允许我复制游戏对象。我需要能够创建一个与我拥有的相同的游戏对象。这存在于棋盘游戏的更大实现中,其中包含其他几个 classes,例如 Board 和 Space。
我有两个复印机文件class:
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
Duplicator.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>()
;
我不断收到的错误是:
no matching constructor for initialization of 'Game'
游戏 class 包含在两个文件中。
Game.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
#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 class.
中为 Game 提供一个构造函数您必须将 RCPP_EXPOSED_CLASS(...)
移至头文件,至少当您想将 class 用作参数或在其他编译单元中输入 return 时。否则编译器不知道,例如Game
可以转换为 SEXP
,反之亦然。