简体   繁体   English

如何将我写入的对象传递给另一个类的构造函数?

[英]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.

相关问题 如何使用构造函数初始化另一个类中的对象? - How can I initialize with a constructor an object from another class? 我如何在一个类中声明一个对象,该对象在其构造函数中需要另一个? - How can I declare an object in a class, that needs another in its constructor? 如何在另一个类的构造函数中初始化一个对象? - How can I initialize an object in the constructor of another class? 如何将类方法传递给另一个函数,例如线程构造函数中发生的情况 - How can I pass a class method to another function like what happen in thread constructor 将对象传递给另一个类的构造函数 - Pass object to constructor of another class 如何将我的类传递给它自己的构造函数? - How can I pass my class into its own constructor? 如何将同一个对象传递给另一个对象的另一个方法? - How can I pass the same object to another method of another object? 我可以将一个类中的函数传递给另一个类的构造函数,并将其存储在那里以便以后调用吗? C ++ - Can i pass a function from a class to a constructor of another class and store it there to call later? C++ 如何将一个对象作为另一个类构造函数的参数传递? - How to pass an object as another class constructor's parameter? 如何将“&player”对象传递给另一个继承的类构造函数(“ score”)? - How to pass “&player” object into another inherited class constructor (“score”)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM