Reputation: 1022
I have been recently working on a project that includes porting TONS of Java over to C++. The conversion isn't to hard with Qt as I only have to change some of the 'ways' that the code works to cooperate with Qt/C++ classes. Only a few issues though.
I have a file called constants.h
this is a normal header file with a top-level class of, you guessed it, Constants
. Nested in there are many other classes. I want to be able to access a nested class from that top-level class. Such as: accessing ErrorCode
from Constants
. I have followed a few thread but I am probably looking in all the wrong corners. Here is my code:
Interface.cpp (MainWindow):
#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 (Classes)
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;
}
};
};
Any help is greatly appreciated! Thank you.
Upvotes: 1
Views: 73
Reputation: 5207
If you want to call methods/constructors from outside the class, then they can't be private
. Private means only code of that class or classes/functions declared as friend
can call it.
In your case a private ErrorCode
constructor can not be called from Interface
unless you declare Interface
to be a friend class
in ErrorCode
:
class ErrorCode
{
friend class Interface;
};
But in your case I don't see any reason for the constructor being private
in the first place and would just make it public
.
Upvotes: 1