每个人,我需要帮助将Java中的一些代码转换为C ++。如何将类实例化为从Java到C ++的私有类型?这就是我的意思,我是一些代码:
第一个问题:解决了。
第二个问题:这个java代码是我想要转换的代码,你觉得我的代码有什么问题?
Java中的原始代码:
public class ClueReasoner
{
private int numPlayers;
private int playerNum;
private int numCards;
private SATSolver solver;
private String caseFile = "cf";
private String[] players = {"sc", "mu", "wh", "gr", "pe", "pl"};
private String[] suspects = {"mu", "pl", "gr", "pe", "sc", "wh"};
private String[] weapons = {"kn", "ca", "re", "ro", "pi", "wr"};
private String[] rooms = {"ha", "lo", "di", "ki", "ba", "co", "bi", "li", "st"};
private String[] cards;
public ClueReasoner()
{
numPlayers = players.length;
// Initialize card info
cards = new String[suspects.length + weapons.length + rooms.length];
int i = 0;
for (String card : suspects)
cards[i++] = card;
for (String card : weapons)
cards[i++] = card;
for (String card : rooms)
cards[i++] = card;
numCards = i;
// Initialize solver
solver = new SATSolver();
addInitialClauses();
}
private int getPlayerNum(String player)
{
if (player.equals(caseFile))
return numPlayers;
for (int i = 0; i < numPlayers; i++)
if (player.equals(players[i]))
return i;
System.out.println("Illegal player: " + player);
return -1;
}
private int getCardNum(String card)
{
for (int i = 0; i < numCards; i++)
if (card.equals(cards[i]))
return i;
System.out.println("Illegal card: " + card);
return -1;
}
private int getPairNum(String player, String card)
{
return getPairNum(getPlayerNum(player), getCardNum(card));
}
private int getPairNum(int playerNum, int cardNum)
{
return playerNum * numCards + cardNum + 1;
}
我的代码:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void Scene_Reasoner() {
int numPlayer;
int playerNum;
int cardNum;
string filecase = "Case: ";
string players [] = {"sc", "mu", "wh", "gr", "pe", "pl"};
string suspects [] = {"mu", "pl", "gr", "pe", "sc", "wh"};
string weapons [] = {"kn", "ca", "re", "ro", "pi", "wr"};
string rooms[] = {"ha", "lo", "di", "ki", "ba", "co", "bi", "li", "st"};
string cards [0];
};
void Scene_Reason_Base () {
numPlayer = players.length;
// Initialize card info
cards = new String[suspects.length + weapons.length + rooms.length];
int i = 0;
for (String card : suspects)
cards[i++] = card;
for (String card : weapons)
cards[i++] = card;
for (String card : rooms)
cards[i++] = card;
cardNum = i;
};
private int getCardNum (string card) {
for (int i = 0; i < numCards; i++)
if (card.equals(cards[i]))
return i;
cout << "Illegal card: " + card <<endl;
return -1;
};
private int getPairNum(String player, String card)
{
return getPairNum(getPlayerNum(player), getCardNum(card));
};
private int getPairNum(int playerNum, int cardNum)
{
return playerNum * numCards + cardNum + 1;
};
int main () {
return 0;
}
答案 0 :(得分:1)
您是否在C ++中实现了SATSolver?如果是这样,那么有一个示例代码:
class Foo
{
private:
int numPlayers;
int playerNum;
int numCards;
SATSolver solver;
// etc
};
答案 1 :(得分:1)
如果您要做的是在另一个类中声明SATSOlver
的私有实例,您可以这样做:
文件SATSolver.h
class SATSolver {
// methods, members, etc....
};
文件Foo.h
#include "SatSolver.h" // get the SATSolver declaration
class Foo
{
private: // everything that follows is private access
SATSolver solver;
public: // from here on, everything is public
// more code
private: // and now, all that follows is private again
// stuff
}
请注意,在C ++中,访问说明符仅允许在类或结构中使用。您不能将它们放在函数内部,也不能在类或结构之外限定类型或值。
注意在C ++中,默认情况下class
具有私有访问权限(和继承,但这超出了本问题的范围):
class Bar {
SATSolver solver; // private
}
答案 2 :(得分:0)
您的代码破坏了我们的转换器 - 我们修复了它,可编译的C ++等价物如下:
#include <string>
class ClueReasoner
{
private:
int numPlayers;
int playerNum;
int numCards;
SATSolver *solver;
std::string caseFile;
std::string players[6];
std::string suspects[6];
std::string weapons[6];
std::string rooms[9];
//ORIGINAL LINE: private String[] cards;
//JAVA TO C++ CONVERTER WARNING: Since the array size is not known in this declaration, Java to C++ Converter has converted this array to a pointer. You will need to call 'delete[]' where appropriate:
std::string *cards;
public:
ClueReasoner()
{
InitializeInstanceFields();
numPlayers = sizeof(players) / sizeof(players[0]);
// Initialize card info
cards = new std::string[sizeof(suspects) / sizeof(suspects[0]) + sizeof(weapons) / sizeof(weapons[0]) + sizeof(rooms) / sizeof(rooms[0])];
int i = 0;
for (std::string::const_iterator card = suspects->begin(); card != suspects->end(); ++card)
cards[i++] = *card;
for (std::string::const_iterator card = weapons->begin(); card != weapons->end(); ++card)
cards[i++] = *card;
for (std::string::const_iterator card = rooms->begin(); card != rooms->end(); ++card)
cards[i++] = *card;
numCards = i;
// Initialize solver
solver = new SATSolver();
addInitialClauses();
}
private:
int getPlayerNum(const std::string &player)
{
if (player == caseFile)
return numPlayers;
for (int i = 0; i < numPlayers; i++)
if (player == players[i])
return i;
std::cout << "Illegal player: " + player << std::endl;
return -1;
}
int getCardNum(const std::string &card)
{
for (int i = 0; i < numCards; i++)
if (card == cards[i])
return i;
std::cout << "Illegal card: " + card << std::endl;
return -1;
}
int getPairNum(const std::string &player, const std::string &card)
{
return getPairNum(getPlayerNum(player), getCardNum(card));
}
int getPairNum(int playerNum, int cardNum)
{
return playerNum * numCards + cardNum + 1;
}
void InitializeInstanceFields()
{
caseFile = "cf";
std::string temp_players[6] = {"sc", "mu", "wh", "gr", "pe", "pl"};
for (int element = 0; element < sizeof(temp_players) / sizeof(temp_players[0]); element++)
players[element] = temp_players[element];
std::string temp_suspects[6] = {"mu", "pl", "gr", "pe", "sc", "wh"};
for (int element = 0; element < sizeof(temp_suspects) / sizeof(temp_suspects[0]); element++)
suspects[element] = temp_suspects[element];
std::string temp_weapons[6] = {"kn", "ca", "re", "ro", "pi", "wr"};
for (int element = 0; element < sizeof(temp_weapons) / sizeof(temp_weapons[0]); element++)
weapons[element] = temp_weapons[element];
std::string temp_rooms[9] = {"ha", "lo", "di", "ki", "ba", "co", "bi", "li", "st"};
for (int element = 0; element < sizeof(temp_rooms) / sizeof(temp_rooms[0]); element++)
rooms[element] = temp_rooms[element];
}
};