【问题标题】:Creating an object with another object as parameters in constructor在构造函数中使用另一个对象作为参数创建一个对象
【发布时间】:2014-01-30 03:25:15
【问题描述】:

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

BoggleGame::BoggleGame(Dictionary dictionaryIN, GameBoard gameboardIN)

Dictionary 和 GameBoard 的构造器看起来像

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;
};

【问题讨论】:

  • 你能发布给出错误的行吗?
  • 您正在尝试在某处调用默认 ctor(Dictionary::Dictionary() 所指的任何行),但您有一个用户定义的 ctor,因此它会抑制默认 ctor 的生成。

标签: c++ object constructor


【解决方案1】:

错误在于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.

如您所见,差别很大。

【讨论】:

  • 如果这可行,我假设 GameBoard 也有类似的情况?我也得到了同样的错误。
【解决方案2】:

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

假设你做了这样的事情:

类 BoggleGame{

私人: 字典dico;

}

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

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

  • 在 Dictionary 类中声明一个不带参数的构造函数
  • 将 dico 声明为指针(意思是 Dictionary * dico)

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

【讨论】:

  • 是的,这可以解决我的编译错误,但这对我的问题没有好处,因为我已经在 main 中创建了一个字典对象,我将其传递给 BoggleGame 的构造函数。跨度>
  • 在这种情况下,只需创建一个没有正文的 Dictionary 的默认构造函数并保持这样。
  • 好的,现在似乎修复了我的编译错误,看看它是否符合我的要求(或至少接近它)
猜你喜欢
  • 2019-12-21
  • 1970-01-01
  • 1970-01-01
  • 2019-03-13
  • 2012-08-04
  • 1970-01-01
  • 2012-07-06
  • 1970-01-01
  • 2020-02-08
相关资源
最近更新 更多