2017-02-14 7 views
1

最近、C++にJavaのTONSを移植することを含むプロジェクトに取り組んでいます。 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と宣言されているクラス/ファンクションだけを意味します。あなたがInterfaceを宣言しない限り、あなたの場合は

はプライベートErrorCodeコンストラクタがあることをInterfaceから呼び出すことはできませんfriend classErrorCode中:

class ErrorCode 
{ 
    friend class Interface; 
}; 

しかし、あなたの場合には、私は、コンストラクタのためにどのような理由があることが表示されませんprivateが最初にあり、それはちょうどpublicになります。

関連する問題