2017-02-14 73 views
1

我最近正在開發一個項目,其中包括將Java的TONS移植到C++。 Qt的轉換並不困難,因爲我只需要改變代碼與Qt/C++類合作的一些'途徑'。只有少數問題。C++如何訪問從Java移植的嵌套類

我有一個名爲constants.h的文件,這是一個正常的頭文件,頂級類別,你猜對了,Constants。嵌套在其他類中。我希望能夠從該頂級類訪問嵌套類。如:從Constants訪問ErrorCode。我跟隨了幾條線索,但我可能正在尋找所有錯誤的角落。這裏是我的代碼:

Interface.cpp(主窗口):

#include "interface.h" 
#include "ui_interface.h" 
#include <QDebug> 
#include "constants.h" 

Interface::Interface(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::Interface) 
{ 
    ui->setupUi(this); 
    //Method 1: 
    //Constants::ErrorCode(QStringList list); 

    //Method 2: 
    //Constants con; 
    //con.ErrorCode er = con.ErrorCode(QStringList list); 
    //or 
    //con::ErrorCode er = con::ErrorCode(QStringList list); 
} 

Interface::~Interface() 
{ 
    delete ui; 
} 

Constants.h(類)

class Constants 
{ 
public: 
class ErrorCode : Localizable 
    { 
     QStringList ACCESS_DENIED = QStringList() << "ad" << "Access denied."; 
     QStringList ALREADY_STARTED = QStringList() << "as" << "The game has already started."; 
     QStringList ALREADY_STOPPED = QStringList() << "aS" << "The game has already stopped."; 
     QStringList BAD_OP = QStringList() << "bo" << "Invalid operation."; 
     QStringList BAD_REQUEST = QStringList() << "br" << "Bad request."; 
     QStringList BANNED = QStringList() << "B&" << "Banned."; 
     QStringList CANNOT_JOIN_ANOTHER_GAME = QStringList() << "cjag" << "You cannot join another game."; 
     QStringList CARDCAST_INVALID_ID = QStringList() << "cii" << "Invalid Cardcast ID. Must be exactly 5 characters."; 
     QStringList DO_NOT_HAVE_CARD = QStringList() << "dnhc" << "You don't have that card."; 
     QStringList GAME_FULL = QStringList() << "gf" << "That game is full. Join another."; 
     QStringList INVALID_CARD = QStringList() << "ic" << "Invalid card specified."; 
     QStringList INVALID_GAME = QStringList() << "ig" << "Invalid game specified."; 
     QStringList INVALID_NICK = QStringList() << "in" << "Nickname must contain only upper and lower case letters, " 
             "numbers, or underscores, must be 3 to 30 characters long, and must not start with a " 
             "number."; 
     QStringList MESSAGE_TOO_LONG = QStringList() << "mtl" << "Messages cannot be longer than 200 characters."; 
     QStringList NICK_IN_USE = QStringList() << "niu" << "Nickname is already in use."; 
     QStringList NO_CARD_SPECIFIED = QStringList() << "ncs" << "No card specified."; 
     QStringList NO_GAME_SPECIFIED = QStringList() << "ngs" << "No game specified."; 
     QStringList NO_MSG_SPECIFIED = QStringList() << "nms" << "No message specified."; 
     QStringList NO_NICK_SPECIFIED = QStringList() << "nns" << "No nickname specified."; 
     QStringList NO_SESSION = QStringList() << "ns" << "Session not detected. Make sure you have cookies enabled."; 
     QStringList NO_SUCH_USER = QStringList() << "nsu" << "No such user."; 
     QStringList NOT_ADMIN = QStringList() << "na" << "You are not an administrator."; 
     /*QStringList NOT_ENOUGH_CARDS = QStringList() << "nec" << "You must add card sets containing at least " 
              + Game.MINIMUM_BLACK_CARDS + " black cards and " + Game.MINIMUM_WHITE_CARDS_PER_PLAYER 
              + " times the player limit white cards.";*/ 
     QStringList NOT_ENOUGH_PLAYERS = QStringList() << "nep" << "There are not enough players to start the game."; 
     QStringList NOT_GAME_HOST = QStringList() << "ngh" << "Only the game host can do that."; 
     QStringList NOT_IN_THAT_GAME = QStringList() << "nitg" << "You are not in that game."; 
     QStringList NOT_JUDGE = QStringList() << "nj" << "You are not the judge."; 
     QStringList NOT_REGISTERED = QStringList() << "nr" << "Not registered. Refresh the page."; 
     QStringList NOT_YOUR_TURN = QStringList() << "nyt" << "It is not your turn to play a card."; 
     QStringList OP_NOT_SPECIFIED = QStringList() << "ons" << "Operation not specified."; 
     QStringList RESERVED_NICK = QStringList() << "rn" << "That nick is reserved."; 
     QStringList SERVER_ERROR = QStringList() << "serr" << "An error occured on the server."; 
     QStringList SESSION_EXPIRED = QStringList() << "se" << "Your session has expired. Refresh the page."; 
     QStringList TOO_FAST = QStringList() << "tf" << "You are chatting too fast. Wait a few seconds and try again."; 
     QStringList TOO_MANY_GAMES = QStringList() << "tmg" << "There are too many games already in progress. Either join " 
             "an existing game, or wait for one to become available."; 
     QStringList TOO_MANY_USERS = QStringList() << "tmu" << "There are too many users connected. Either join another server, or " 
             "wait for a user to disconnect."; 
     QStringList WRONG_PASSWORD = QStringList() << "wp" << "That password is incorrect."; 
    private: 
     QString code; 
     QString message; 
     ErrorCode(const QStringList dataList) 
     { 
      this->code = dataList[0]; 
      this->message = dataList[1]; 
     } 
    public: 
     virtual QString toString() 
     { 
      return code; 
     } 
     virtual QString getString() 
     { 
      return message; 
     } 
    }; 
}; 

任何幫助,不勝感激!謝謝。

+0

不確定它在這一點上有幫助,但你可能想看看[Qt Jambi](http://qtjambi.org/)。 –

+0

@ElliottFrisch一點都不差!我研究過這個,但我不想在Java中使用Qt類。這個項目只是爲了測試自己。我特別想將代碼移植到C++中並使其功能完整。 –

+0

然後你需要去嵌套這些類,因爲(據我所知)C++沒有這些類。你發佈了C++,聽起來像你應該發佈Java的東西。 –

回答

1

如果你想從課堂外調用方法/構造函數,那麼它們不能是private。私有隻意味着聲明爲friend的該類或類/函數的代碼可以調用它。

在你的情況下,私人ErrorCode構造不能從Interface除非你聲明Interface稱爲是一個friend classErrorCode

class ErrorCode 
{ 
    friend class Interface; 
}; 

但在你的情況下,我看不出有任何理由構造是private在第一個地方,只會使它public