2014-01-30 35 views
1

因此,我正在製作一個程序,它將查找所有可能的單詞(由字典定義)的一個boggle遊戲(谷歌它,如果你不知道它是什麼是)。無論如何,我有3類/對象:字典,遊戲板和bogglegame。 bogglegame應該梳理字典和遊戲板,並找到所有合法的單詞。我對bogglegame構造看起來像用另一個對象作爲參數在構造函數中創建一個對象

BoggleGame::BoggleGame(Dictionary dictionaryIN, GameBoard gameboardIN)

和詞典和遊戲鍵盤的contsrtuctors樣子

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

,當我嘗試編譯我得到,說:「錯誤的錯誤:沒有匹配函數調用'Dictionary :: Dictionary()「

我想能夠通過在詞典ry和gameboard對象從主存入到構造函數中,並將它們存儲爲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; 
}; 
+0

你可以發佈給出錯誤的行嗎? – Brian

+1

你正在嘗試在某處調用默認的ctor(無論'Dictionary :: Dictionary()'所指的行是什麼),但是你有一個用戶定義的ctor,所以它禁止生成默認的ctor。 –

回答

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. 

大的差異,你可以看到。

+0

,如果這個工作,我假設爲GameBoard類似的事情?我也得到了同樣的錯誤。 – undermark5

+0

@ undermark5這是一回事,是的。 – 2014-01-30 04:27:27

0

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

比方說,你做某事像這樣:

類BoggleGame {

私人: 字典迪科;

}

在這種情況下,會出現一個編譯錯誤,如果構造字典::字典()不存在。

所以我覺得,你的問題的解決方案應該是兩種:

  • 聲明無參數的構造函數中Dictionary類
  • 申報迪科爲指針(也就是平均字典*迪科)

但是,如果您發佈代碼會更好。

+0

是的,這將解決我的編譯錯誤,但它不會有利於我的問題,因爲我已經創建了一個字典對象的主要傳遞給BoggleGame的構造函數。 – undermark5

+0

在這種情況下,只需創建Dictionary的默認構造方法,不需要任何內容​​,就可以這樣做。 – user3173787

+0

好吧,似乎現在修復我的編譯錯誤,看看它是否做到了我想要的(或者至少接近它) – undermark5

相關問題