En una clase Board
, hago una función de movimientos (movimientos
) legales que especifica qué movimientos puedo hacer en el tablero. Moves
es una matriz de dos movimientos que puedo hacer o anular. Lo explico en privado.
Sin embargo, no parece que sea la forma de explicarlo:
main2.cpp:126:14: error: ‘moves’ has not been declared
legalMoves(moves);
^
El mensaje de error completo es:
mike@mike-thinks:~/NestedMonteCarlo/NestedForBinaryTrees$ g++ main2.cpp -o NestedMonteCarlo
main2.cpp:126:14: error: ‘moves’ has not been declared
legalMoves(moves); // error: ‘moves’ has not been declared : Yes it has !
^
main2.cpp:126:19: error: ISO C++ forbids declaration of ‘legalMoves’ with no type [-fpermissive]
legalMoves(moves); // error: ‘moves’ has not been declared : Yes it has !
^
main2.cpp:133:3: error: ‘vector’ does not name a type
vector<*node> moves(2,NULL);
^
main2.cpp: In constructor ‘Board::Board(btree, node)’:
main2.cpp:153:9: error: ‘left’ was not declared in this scope
if(n->*left != NULL)
^
main2.cpp:154:3: error: ‘moves’ was not declared in this scope
moves[0] = n->*left;//DONE? array of the left and right positions from curren
^
main2.cpp:155:3: error: ‘moves’ was not declared in this scope
moves[1] = n->*right;
^
main2.cpp:155:18: error: ‘right’ was not declared in this scope
moves[1] = n->*right;
^
main2.cpp: At global scope:
main2.cpp:160:28: error: ‘int Board::legalMoves’ is not a static data member of ‘class Board’
int Board::legalMoves(moves){
^
main2.cpp:160:23: error: ‘moves’ was not declared in this scope
int Board::legalMoves(moves){
^
main2.cpp:160:29: error: expected ‘,’ or ‘;’ before ‘{’ token
int Board::legalMoves(moves){
^
Entonces, ¿cómo puede una función pública de una clase considerar atributos privados como argumentos?
Escribí todo el código en un archivo: main2.cpp:
#define NULL 0
#include <vector>
// main2
struct node
{
int key_value;
node *left;
node *right;
};
class btree
{
public:
btree();
~btree();
void insert(int key);
node *search(int key);
void destroy_tree();
private:
void destroy_tree(node *leaf);
void insert(int key, node *leaf);
node *search(int key, node *leaf);
node *root;
};
btree::btree()
{
root=NULL;
}
btree::~btree()
{
destroy_tree();
}
void btree::destroy_tree(node *leaf)
{
if(leaf!=NULL)
{
destroy_tree(leaf->left);
destroy_tree(leaf->right);
delete leaf;
}
}
void btree::insert(int key, node *leaf)
{
if(key< leaf->key_value)
{
if(leaf->left!=NULL)
insert(key, leaf->left);
else
{
leaf->left=new node;
leaf->left->key_value=key;
leaf->left->left=NULL; //Sets the left child of the child node to null
leaf->left->right=NULL; //Sets the right child of the child node to null
}
}
else if(key>=leaf->key_value)
{
if(leaf->right!=NULL)
insert(key, leaf->right);
else
{
leaf->right=new node;
leaf->right->key_value=key;
leaf->right->left=NULL; //Sets the left child of the child node to null
leaf->right->right=NULL; //Sets the right child of the child node to null
}
}
}
void btree::insert(int key)
{
if(root!=NULL)
insert(key, root);
else
{
root=new node;
root->key_value=key;
root->left=NULL;
root->right=NULL;
}
}
node *btree::search(int key, node *leaf)
{
if(leaf!=NULL)
{
if(key==leaf->key_value)
return leaf;
if(key<leaf->key_value)
return search(key, leaf->left);
else
return search(key, leaf->right);
}
else return NULL;
}
node *btree::search(int key)
{
return search(key, root);
}
void btree::destroy_tree()
{
destroy_tree(root);
}
class Board{
public:
Board();
~Board();
//a Board is made from a binary tree. Furthermore he has a score, a length
Board(btree,node);
//And he has possible blows
legalMoves(moves); // error: ‘moves’ has not been declared : Yes it has ! Just below !
private:
//member variables
int length;
int score;
// create move ? Array of the following possibilities : left/right ? Or none if we reached the end
vector<*node> moves(2,NULL);
};
Board::Board(){
length = 0;
score = 0;
};
Board::~Board(){
};
Board::Board(btree b, node n){
length = NULL; //TO-DO : number of nodes which have leaves BUT how to count them ?
score = n.key_value; //DONE? : number on the current node FROM node
if(n->*left != NULL)
moves[0] = n->*left;//DONE? array of the left and right positions from current state //node *left; and node *right;
moves[1] = n->*right;
};
// gives the number of leaves we can play : either 2 or 0 in the binary-tree case.
int Board::legalMoves(moves){
//If there is leaves there is at least two moves.
if(n->*left != NULL){
return 2;
}//otherwise there is no possibilities
else{
return 0;
}
};
/**/
double playout (Board * board) {
Move listeCoups [MaxLegalMoves];
while (true) {
int nb = board->legalMoves (listeCoups);
if ((nb == 0) || board->terminal ())
return board->score ();
int n = rand () % nb;
board->play (listeCoups [n]);
if (board->length >= MaxPlayoutLength - 20) {
return 0;
}
}
};
#include <float.h>
double bestScoreNested = -DBL_MAX;
int lengthBestRollout [10];
double scoreBestRollout [10];
Move bestRollout [10] [MaxPlayoutLength];
Board bestBoard;
// What is int n ?
double nested (Board & board, int n) {
int nbMoves = 0;
Move moves [MaxLegalMoves];
lengthBestRollout [n] = -1;
scoreBestRollout [n] = -DBL_MAX;
float res;
while (true) {
if (board.terminal ())
return 0.0;
//return board.score ();
nbMoves = board.legalMoves (moves);
for (int i = 0; i < nbMoves; i++) {
Board b = board;
b.play (moves [i]);
//what is n ?
if (n == 1)
playout (&b);
else
nested (b, n - 1);
double score = b.score ();
if (score > scoreBestRollout [n]) {
scoreBestRollout [n] = score;
lengthBestRollout [n] = b.length;
for (int k = 0; k < b.length; k++)
bestRollout [n] [k] = b.rollout [k];
// what is n ?
if (n > 3) {
for (int t = 0; t < n - 1; t++)
fprintf (stderr, "\t");
fprintf (stderr, "n = %d, progress = %d, score = %f\n", n, board.length, scoreBestRollout [n]);
int depth = 0;
b.print (stderr);
fprintf (stderr, "\n");
}
if ((n > 1) && (score > bestScoreNested)) {
bestScoreNested = score;
fprintf (stderr, "best score = %f\n", score);
b.print (stderr);
fprintf (stderr, "\n");
bestBoard = b;
}
}
}
board.play (bestRollout [n] [board.length]);
}
return 0.0;
};
/**/
int main(int argc, char *argv []) {
btree b;
b.insert(5);
b.insert(6);
b.insert(8);
b.insert(10);
b.insert(11);
b.insert(14);
b.insert(18);
// create board with b
board boa(b)
nestedSimple(b, 3);
bestBoard.print (stderr);
fprintf (stderr, "best score %lf\n", bestBoard.score ());
return(0);
}
legalMoves(moves); // error: ‘moves’ has not been declared : Yes it has ! Just below !
Esta no es la declaración de una función, le falta el tipo devuelto y el tipo del argumento. Deberías comenzar con algún libro que describa los rudimentos del lenguaje antes de aventurarte más lejos.