簡體   English   中英

在構造函數中使用另一個對象作為參數創建一個對象

[英]Creating an object with another object as parameters in constructor

因此,我正在編寫一個程序,該程序將查找boggle游戲(如果您不知道它是什么,可以用Google搜索)的所有可能的單詞(由字典定義)。 無論如何,我有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;
};

也許這就是在BoggleGame類中聲明Dictionary對象的方式。

假設您做了這樣的事情:

BoggleGame類{

private:字典dico;

}

在這種情況下,如果構造函數Dictionary :: Dictionary()不存在,則會出現編譯錯誤。

因此,我認為,您的問題的解決方案應該是:

  • 在Dictionary類中聲明一個沒有參數的構造函數
  • 聲明dico作為指針(這意味着Dictionary * dico)

但是如果發布代碼會更好。

錯誤在於BoggleGame的構造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.

您可以看到很大的不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM