gpt4 book ai didi

java - C++ 如何访问从 Java 移植的嵌套类

转载 作者:行者123 更新时间:2023-11-30 05:17:12 29 4
gpt4 key购买 nike

我最近一直在从事一个项目,其中包括将大量 Java 移植到 C++。使用 Qt 进行转换并不难,因为我只需更改代码与 Qt/C++ 类协作的一些“方式”。不过只有几个问题。

我有一个名为 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;
}
};
};

非常感谢任何帮助!谢谢。

最佳答案

如果您想从类外部调用方法/构造函数,则它们不能是私有(private)。私有(private)意味着只有该类的代码或声明为 friend 的类/函数可以调用它。

在您的情况下,私有(private)ErrorCode 构造函数无法从Interface 调用,除非您将Interface 声明为友元类ErrorCode 中:

class ErrorCode
{
friend class Interface;
};

但在您的情况下,我看不出有任何理由让构造函数首先成为private,而只是将其设为public

关于java - C++ 如何访问从 Java 移植的嵌套类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42217222/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com