使用另一个对象作为构造函数中的参数创建对象

时间:2014-01-30 03:25:16

标签: c++ object constructor

所以我正在制作一个程序,可以找到一个博格游戏的所有可能的单词(由字典定义)(谷歌,如果你不知道它是什么)。无论如何,我有3个类/对象:字典,游戏板和bogglegame。 bogglegame应该梳理字典和游戏板,并找到所有合法的单词。我的bogglegame构造函数看起来像

BoggleGame::BoggleGame(Dictionary dictionaryIN, GameBoard gameboardIN)

和Dictionary和GameBoard的contsrtuctors看起来像

Dictionary::Dictionary(set<string> wordsInDictionaryIN, unsigned maxLengthIN) GameBoard::GameBoard(vector<vector<string> > gamestateIN, unsigned boardSizeIN)

当我尝试编译时,我得到一个错误,上面写着“错误:没有匹配函数来调用'Dictionary :: Dictionary()”

我希望能够将字典和游戏板对象从main传递到构造函数中,并将它们存储为BoggleGame对象的私有成员,从而有效地使BoggleGame对象成为2个对象的对象。

编辑:发布代码

BoggleGame的构造函数

#include "BoggleGame.h"

BoggleGame::BoggleGame(Dictionary dictionaryIN, GameBoard gameboardIN)
{
    dictionary = dictionaryIN;
    gameboard = gameboardIN;
}

#pragma once

#include "Dictionary.h"
#include "GameBoard.h"

class BoggleGame
{
public:
    BoggleGame(Dictionary dictioanryIN, GameBoard gameboardIN);
    void foundWord(string wordIN);
    string findTheWords(string w, unsigned row, unsigned column);
    set<string> getTheFoundWords() {return foundWords;}
    bool isInDictionary(string word);
    bool isOffBoard(unsigned row, unsigned column);
    bool usedTile(unsigned row, unsigned column);
    vector<vector<string> > getTheGameBoard(){gameboard.getGameBoard();}

private:
    Dictionary dictionary;
    GameBoard gameboard;
    set<string> foundWords;
};

#pragma once
#include <set>
#include <vector>
#include <string>
#include <iterator>

using std::set;
using std::string;
using std::vector;

class Dictionary
{
public:
    Dictionary(set<string> wordsInDictionaryIN, unsigned maxLengthIN);
    bool isInDictionary(string wordIN);
    void foundWord (string wordIN);
    string findTheWords(string w, unsigned row, unsigned column);
    unsigned getMaxLength() {return maxLength;}

private:
    set<string> wordsInDictionary;
    unsigned maxLength;
    set<string> foundWords;

};

#pragma once
#include <vector>
#include <string>

using std::string;
using std::vector;

class GameBoard
{
public:
    GameBoard(vector<vector<string> > gamestateIN, unsigned boardSizeIN);
    bool outOfBoard(unsigned row, unsigned column);
    bool getTileUseState(unsigned row, unsigned column){return usedTiles.at(row).at(column);}
    void setTileUsed(unsigned row, unsigned column);
    vector<vector<string> > getGameBoard(){return gamestate;}
    unsigned getSize(){return boardSize;}
    vector<vector<bool> > getUsedTiles() {return usedTiles;}
    string readTile(unsigned row, unsigned column) {return gamestate.at(row).at(column);}
    void previousState(vector<vector<bool> > previous) {usedTiles = previous;}

private:
    vector<vector<string> > gamestate;
    vector<vector<bool> > usedTiles;
    unsigned boardSize;
};

2 个答案:

答案 0 :(得分:0)

也许这就是你在BoggleGame类中声明Dictionary对象的方式。

让我们说你做的是这样的:

类BoggleGame {

私人:     字典dico;

}

在这种情况下,如果构造函数Dictionary :: Dictionary()不存在,则会出现编译错误。

所以我认为,你的问题的解决方案应该是:

  • 在Dictionary类
  • 中声明一个没有参数的构造函数
  • 将dico声明为指针(即平均字典* dico)

但是如果发布你的代码会更好。

答案 1 :(得分:0)

错误在于BoggleGame的构造函数。您没有Dictionary的默认构造函数。您可以在构造函数中使用初始化列表:

BoggleGame::BoggleGame(Dictionary _dictionary)
    : dictionary{_dictionary}

或者,为Dictionary提供默认构造函数:

Dictionary() = default;
Dictionary() { }

发生错误的原因是因为dictionary是默认构造的。将新值分配给dictionary时,您正在使用复制赋值运算符。通过使用构造函数初始化列表,可以直接初始化dictionary。为了证明:

BoggleGame::BoggleGame(Dictionary _dictionary)
{
    dictionary = _dictionary;
}

Default.
Copy assignment.

BoggleGame::BoggleGame(Dictionary _dictionary)
    : dictionary{_dictionary}
    {
    }

Copy constructor.

你可以看到很大的不同。