#include "Map.hh" #include int main() { const std::string filename = "map.txt"; Sokoban::Map map; if (!map.load(filename)) { std::cerr << "Error while loading: " << filename << std::endl; return 1; } std::cout << "Rows: " << map.getNbRows() << std::endl << "Cols: " << map.getNbCols() << std::endl << "Gems: " << map.getNbGems() << std::endl << map << std::endl; std::cout << "Test (3, 2) is: " << map(3, 2) << std::endl << " (6, 5) is: " << map(6, 5) << std::endl << " (7, 4) is: " << map(7, 4) << std::endl; return 0; }
#ifndef MAP_HH_ # define MAP_HH_ # include <string> # include <vector> namespace Sokoban { class Map { public: Map(); bool load(const std::string& filename); void display(std::ostream& out) const; unsigned int getNbRows() const; unsigned int getNbCols() const; unsigned int getNbGems() const; void clear(); char operator()(unsigned int x, unsigned y) const; private: unsigned int _row; unsigned int _column; unsigned int _gemNumber; std::vector<char> _data; }; std::ostream& operator<<(std::ostream& out, const Map& map); } // Sokoban #endif /* !MAP_HH_ */
#include "Map.hh" #include <fstream> #include <cassert> namespace Sokoban { Map::Map() : _row(0), _column(0), _gemNumber(0), _data() { } bool Map::load(const std::string& filename) { std::ifstream file(filename.c_str()); if (!file) return false; file >> _column >> _row >> _gemNumber; file.ignore(1, '\n'); if (_row < = 0 || _column <= 0) return false; _data.resize(_row * _column); std::string line; for (unsigned int x = 0; std::getline(file, line) && x < _row; ++x) for (unsigned int y = 0; y < _column; ++y) _data[x * _column + y] = line[y]; return true; } unsigned int Map::getNbRows() const { return _row; } unsigned int Map::getNbCols() const { return _column; } unsigned int Map::getNbGems() const { return _gemNumber; } void Map::clear() { _row = 0; _column = 0; _gemNumber = 0; _data.clear(); } void Map::display(std::ostream& out) const { for (unsigned int x = 0; x < _row; ++x) { for (unsigned int y = 0; y < _column; ++y) out << _data[x * _column + y]; out << std::endl; } } char Map::operator()(unsigned int x, unsigned int y) const { assert(x < _row); assert(y < _column); return _data[x * _column + y]; } std::ostream& operator<<(std::ostream& out, const Map& map) { map.display(out); return out; } } // Sokoban
Excuse moi du retard pour la validation de tes réponses, elles m'ont vraiment aidé!
Pour ce qui est des droit sous linux j'ai laissé tomber
J'ai fait des modifs un peu trop grosse sans faire assez de test... résultat ça plante partout (plutôt normal :P)
Je m'amuse donc avec le debuggeur mais là surprise, en debug tout va bien, en release ça plante au départ.
aStar.cpp:280:1: warning: multi-line comment [-Wcomment] In file included from map.hpp:4:0, from aStar.cpp:4: point.hpp:17:32: error: ‘Point::Point’ is not a type In file included from aStar.cpp:4:0: map.hpp:16:9: error: ‘Point::Point’ names the constructor, not the type map.hpp:17:32: error: ‘Point::Point’ is not a type map.hpp:24:9: error: ‘Point::Point’ names the constructor, not the type In file included from aStar.cpp:5:0: node.hpp:13:50: error: ‘Point::Point’ is not a type node.hpp:22:9: error: ‘Point::Point’ names the constructor, not the type node.hpp:23:35: error: ‘Point::Point’ is not a type node.hpp:32:9: error: ‘Point::Point’ names the constructor, not the type In file included from aStar.cpp:6:0: aStar.hpp:5:18: error: ‘Map::Map’ names the constructor, not the type aStar.hpp:5:28: error: ISO C++ forbids declaration of ‘myMap’ with no type [-fpermissive] aStar.hpp:5:41: error: ‘Node::Node’ is not a type aStar.hpp:5:60: error: ‘Point::Point’ is not a type aStar.hpp:6:34: error: ‘lowerWeight’ was not declared in this scope aStar.hpp:6:47: error: expected primary-expression before ‘const’ aStar.hpp:6:70: error: expected primary-expression before ‘int’ aStar.hpp:6:83: error: expression list treated as compound expression in initializer [-fpermissive] aStar.hpp:7:27: error: ‘lowerWeight’ was not declared in this scope aStar.hpp:7:40: error: expected primary-expression before ‘const’ aStar.hpp:7:63: error: expected primary-expression before ‘int’ aStar.hpp:7:76: error: expression list treated as compound expression in initializer [-fpermissive] aStar.hpp:8:35: error: ‘Node::Node’ cannot appear in a constant-expression aStar.hpp:8:40: error: template argument 1 is invalid aStar.hpp:8:40: error: template argument 2 is invalid aStar.hpp:8:69: error: ‘Node::Node’ cannot appear in a constant-expression aStar.hpp:8:74: error: template argument 1 is invalid aStar.hpp:8:74: error: template argument 2 is invalid aStar.cpp:10:18: error: ‘Map::Map’ names the constructor, not the type aStar.cpp:10:28: error: ISO C++ forbids declaration of ‘myMap’ with no type [-fpermissive] aStar.cpp:10:41: error: ‘Node::Node’ is not a type aStar.cpp:10:60: error: ‘Point::Point’ is not a type aStar.cpp: In function ‘void aStar(const int*, int*, int)’: aStar.cpp:15:21: error: ‘Node::Node’ cannot appear in a constant-expression aStar.cpp:15:26: error: template argument 1 is invalid aStar.cpp:15:26: error: template argument 2 is invalid aStar.cpp:15:36: error: invalid type in declaration before ‘;’ token aStar.cpp:16:21: error: ‘Node::Node’ cannot appear in a constant-expression aStar.cpp:16:26: error: template argument 1 is invalid aStar.cpp:16:26: error: template argument 2 is invalid aStar.cpp:16:37: error: invalid type in declaration before ‘;’ token aStar.cpp:18:17: error: ‘lowerWeightNode’ was not declared in this scope aStar.cpp:19:17: error: ‘lastNode’ was not declared in this scope aStar.cpp:21:14: error: request for member ‘push_back’ in ‘openList’, which is of non-class type ‘int’ aStar.cpp:33:22: error: request for member ‘size’ in ‘openList’, which is of non-class type ‘int’ aStar.cpp:37:27: error: request for member ‘size’ in ‘openList’, which is of non-class type ‘int’ aStar.cpp:39:33: error: request for member ‘begin’ in ‘openList’, which is of non-class type ‘int’ aStar.cpp:40:40: error: request for member ‘front’ in ‘openList’, which is of non-class type ‘int’ aStar.cpp:44:40: error: request for member ‘front’ in ‘openList’, which is of non-class type ‘int’ aStar.cpp:45:56: error: request for member ‘begin’ in ‘openList’, which is of non-class type ‘int’ aStar.cpp:48:37: error: request for member ‘end’ in ‘openList’, which is of non-class type ‘int’ aStar.cpp:72:23: error: request for member ‘splice’ in ‘closeList’, which is of non-class type ‘int’ aStar.cpp:72:41: error: request for member ‘begin’ in ‘closeList’, which is of non-class type ‘int’ aStar.cpp:82:62: error: ‘isValidMovement’ cannot be used as a function aStar.cpp:85:21: error: ‘Point::Point’ names the constructor, not the type aStar.cpp:85:34: error: expected ‘;’ before ‘myCoordinate’ aStar.cpp:85:80: error: statement cannot resolve address of overloaded function aStar.cpp:90:29: error: ‘myCoordinate’ was not declared in this scope aStar.cpp:109:42: error: ‘Node::Node’ cannot appear in a constant-expression aStar.cpp:109:47: error: template argument 1 is invalid aStar.cpp:109:47: error: template argument 2 is invalid aStar.cpp:109:59: error: invalid type in declaration before ‘it’ aStar.cpp:109:59: error: invalid use of qualified-name ‘::iterator’ aStar.cpp:109:59: error: expected ‘;’ before ‘it’ aStar.cpp:109:59: error: ‘it’ was not declared in this scope aStar.cpp:109:72: error: request for member ‘begin’ in ‘closeList’, which is of non-class type ‘int’ aStar.cpp:109:95: error: request for member ‘end’ in ‘closeList’, which is of non-class type ‘int’ aStar.cpp:109:100: error: expected ‘)’ before ‘;’ token aStar.cpp:109:104: error: ‘it’ was not declared in this scope aStar.cpp:109:106: error: expected ‘;’ before ‘)’ token aStar.cpp:107:26: warning: unused variable ‘isContainedInCloseList’ [-Wunused-variable] aStar.cpp:273:1: error: expected ‘}’ at end of input aStar.cpp:273:1: error: expected ‘}’ at end of input aStar.cpp:273:1: error: expected ‘}’ at end of input aStar.cpp:273:1: error: expected ‘}’ at end of input aStar.cpp:273:1: error: expected ‘while’ at end of input aStar.cpp:273:1: error: expected ‘(’ at end of input aStar.cpp:273:1: error: expected primary-expression at end of input aStar.cpp:273:1: error: expected ‘)’ at end of input aStar.cpp:273:1: error: expected ‘;’ at end of input aStar.cpp:273:1: error: expected ‘}’ at end of input In file included from map.hpp:4:0, from main.cpp:3: point.hpp:17:32: error: ‘Point::Point’ is not a type In file included from main.cpp:3:0: map.hpp:16:9: error: ‘Point::Point’ names the constructor, not the type map.hpp:17:32: error: ‘Point::Point’ is not a type map.hpp:24:9: error: ‘Point::Point’ names the constructor, not the type In file included from main.cpp:4:0: node.hpp:13:50: error: ‘Point::Point’ is not a type node.hpp:22:9: error: ‘Point::Point’ names the constructor, not the type node.hpp:23:35: error: ‘Point::Point’ is not a type node.hpp:32:9: error: ‘Point::Point’ names the constructor, not the type In file included from main.cpp:5:0: aStar.hpp:5:18: error: ‘Map::Map’ names the constructor, not the type aStar.hpp:5:28: error: ISO C++ forbids declaration of ‘myMap’ with no type [-fpermissive] aStar.hpp:5:41: error: ‘Node::Node’ is not a type aStar.hpp:5:60: error: ‘Point::Point’ is not a type aStar.hpp:6:34: error: ‘lowerWeight’ was not declared in this scope aStar.hpp:6:47: error: expected primary-expression before ‘const’ aStar.hpp:6:70: error: expected primary-expression before ‘int’ aStar.hpp:6:83: error: expression list treated as compound expression in initializer [-fpermissive] aStar.hpp:7:27: error: ‘lowerWeight’ was not declared in this scope aStar.hpp:7:40: error: expected primary-expression before ‘const’ aStar.hpp:7:63: error: expected primary-expression before ‘int’ aStar.hpp:7:76: error: expression list treated as compound expression in initializer [-fpermissive] aStar.hpp:8:35: error: ‘Node::Node’ cannot appear in a constant-expression aStar.hpp:8:40: error: template argument 1 is invalid aStar.hpp:8:40: error: template argument 2 is invalid aStar.hpp:8:69: error: ‘Node::Node’ cannot appear in a constant-expression aStar.hpp:8:74: error: template argument 1 is invalid aStar.hpp:8:74: error: template argument 2 is invalid main.cpp: In function ‘int main()’: main.cpp:14:17: error: ‘root’ was not declared in this scope main.cpp:14:28: error: expected type-specifier main.cpp:14:28: error: expected ‘;’ main.cpp:15:31: error: ‘class Map’ has no member named ‘getPlayerPosition’ main.cpp:17:41: error: cannot call constructor ‘Point::Point’ directly [-fpermissive] main.cpp:17:41: error: for a function-style cast, remove the redundant ‘::Point’ [-fpermissive] main.cpp:19:12: error: type ‘<type error>’ argument given to ‘delete’, expected pointer In file included from map.hpp:4:0, from map.cpp:1: point.hpp:17:32: error: ‘Point::Point’ is not a type In file included from map.cpp:1:0: map.hpp:16:9: error: ‘Point::Point’ names the constructor, not the type map.hpp:17:32: error: ‘Point::Point’ is not a type map.hpp:24:9: error: ‘Point::Point’ names the constructor, not the type map.cpp: In constructor ‘Map::Map(char*)’: map.cpp:80:25: error: ‘playerPosition’ was not declared in this scope map.cpp:80:58: error: cannot call constructor ‘Point::Point’ directly [-fpermissive] map.cpp:80:58: error: for a function-style cast, remove the redundant ‘::Point’ [-fpermissive] map.cpp: At global scope: map.cpp:104:29: error: ‘char Map::getContent’ is not a static member of ‘class Map’ map.cpp:104:35: error: expected ‘)’ before ‘_point’ map.cpp:104:41: error: cannot resolve overloaded function ‘Point’ based on conversion to type ‘char’ map.cpp:104:43: error: expected ‘,’ or ‘;’ before ‘const’ map.cpp:116:1: error: ‘Point::Point’ names the constructor, not the type In file included from node.hpp:6:0, from node.cpp:1: point.hpp:17:32: error: ‘Point::Point’ is not a type In file included from node.cpp:1:0: node.hpp:13:50: error: ‘Point::Point’ is not a type node.hpp:22:9: error: ‘Point::Point’ names the constructor, not the type node.hpp:23:35: error: ‘Point::Point’ is not a type node.hpp:32:9: error: ‘Point::Point’ names the constructor, not the type node.cpp: In constructor ‘Node::Node()’: node.cpp:8:5: error: ‘coordinate’ was not declared in this scope node.cpp:8:34: error: cannot call constructor ‘Point::Point’ directly [-fpermissive] node.cpp:8:34: error: for a function-style cast, remove the redundant ‘::Point’ [-fpermissive] node.cpp: At global scope: node.cpp:14:48: error: ‘Point::Point’ is not a type node.cpp: In constructor ‘Node::Node(int, int, int, int, Node*)’: node.cpp:18:5: error: ‘coordinate’ was not declared in this scope node.cpp: At global scope: node.cpp:54:1: error: ‘Point::Point’ names the constructor, not the type node.cpp:60:33: error: variable or field ‘setCoordinate’ declared void node.cpp:60:39: error: expected ‘)’ before ‘_coordinate’ node.cpp: In member function ‘void Node::display()’: node.cpp:78:39: error: ‘coordinate’ was not declared in this scope In file included from point.cpp:1:0: point.hpp:17:32: error: ‘Point::Point’ is not a type point.cpp:26:20: error: ‘Point::Point’ names the constructor, not the type point.cpp:26:34: error: ISO C++ forbids declaration of ‘myPoint’ with no type [-fpermissive] point.cpp:26:1: error: prototype for ‘Point::Point(const int&)’ does not match any in class ‘Point’ point.hpp:15:9: error: candidates are: Point::Point(const Point&) point.cpp:11:1: error: Point::Point(int, int) point.cpp:4:1: error: Point::Point() point.cpp:42:37: error: declaration of ‘operator==’ as non-function point.cpp:42:37: error: expected ‘)’ before ‘const’
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionJe travaille sous windows et j'ai les mêmes flags de compil que toi
J'ai par contre un problème pour exécuter 'solver' (en utilisant la même commande de compil que toi). J'ai un 'permission denied'.
En créant l'exe sous windows, mon premier problème persiste...
Au passage, j'ai regardé pas mal de tuto sur les #include. Ce qu'il en sort est qu'il ne faut pas trop en mettre dans les .hpp, il y aurait-il une autre façon de faire? (je prend pour exemple node.hpp dans lequel je dois include point.hpp
gdb$ bt
#0 0x00007ffff753b425 in __GI_raise (sig=<optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
#1 0x00007ffff753eb8b in __GI_abort () at abort.c:91
#2 0x00007ffff757939e in __libc_message (do_abort=2, fmt=0x7ffff7683008 "*** glibc detected *** %s: %s: 0x%s ***\n") at ../sysdeps/unix/sysv/linux/libc_fatal.c:201
#3 0x00007ffff7583b96 in malloc_printerr (action=3, str=0x7ffff76831f8 "double free or corruption (fasttop)", ptr=<optimized out>) at malloc.c:5007
#4 0x0000000000403b96 in Node::~Node (this=0x7fffffffdc70, __in_chrg=<optimized out>) at node.cpp:29
#5 0x0000000000401af6 in aStar (myMap=0x7fffffffdde0, root=0x609260, destination=...) at aStar.cpp:152
#6 0x00000000004031c0 in main () at main.cpp:17
// If the node is not in the openList, add it to. Else, replace it if better if (!isContainedInOpenList) { Node myNode(heuristicValue, i, myCoordinate, lowerWeightNode->getMovementCost() +1, lowerWeightNode); openList.push_back(&myNode); ///////////////////////// // Display the added node std::cout << "Added node because not contained in openList :" << std::endl; myNode.display(); std::cout << std::endl << std::endl; }
Node myNode(heuristicValue, i, myCoordinate, lowerWeightNode->getMovementCost() +1, lowerWeightNode); openList.push_back(&myNode);
Node* myNode = new Node(heuristicValue, i, myCoordinate, lowerWeightNode->getMovementCost() +1, lowerWeightNode); openList.push_back(myNode);
#include #include #include <list> #include "map.hpp" #include "node.hpp" #include "aStar.hpp"
// En premier le .hpp qui va avec ton .cpp #include "aStar.hpp" // Vient ensuite tes propres headers #include "map.hpp" #include "node.hpp" // Et enfin les headers systèmes. #include #include #include <list>
Node* root = new Node(); root->setCoordinate(myMap.getPlayerPosition()); aStar(&myMap, root, Point(1,1)); delete root;
Node root; root.setCoordinate(myMap.getPlayerPosition()); aStar(&myMap, &root, Point(1,1));
Merci beaucoup de ton aide, mais je vais refaire les classes moi même, au moins j'apprendrais plus facilement.
Je veux bien un dernier conseil par contre. Même arès le chmod 755 ou le chmod +x, mon 'exe' reste en permission denied. J'ai vu un article sur stackoverflow qui situait le problème sur le fait que mon 'exe' est sur une partition séparée est que les droits que j'ai après le mount ne sont pas suffisant pour executer un programme...
= =2716== HEAP SUMMARY:
2716== in use at exit: 49,404 bytes in 1,525 blocks
2716== total heap usage: 8,717 allocs, 7,192 frees, 238,460 bytes allocated
2716==
2716== 2,028 (144 direct, 1,884 indirect) bytes in 1 blocks are definitely lost in loss record 10 of 18
2716== at 0x4C2AF8E: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
2716== by 0x40A1E7: Sokoban::aStarDiamond(Sokoban::Node&, Sokoban::Point) ( aStarDiamond.cpp:163 )
== 2716== by 0x407392: Sokoban::solve(Sokoban::Map&) (solver.cpp:19)
2716== by 0x40199F: main (main.cpp:28)
2716==
2716== 3,144 (288 direct, 2,856 indirect) bytes in 2 blocks are definitely lost in loss record 13 of 18
2716== at 0x4C2AF8E: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
2716== by 0x40A05A: Sokoban::aStarDiamond(Sokoban::Node&, Sokoban::Point) ( aStarDiamond.cpp:148 )
== 2716== by 0x407392: Sokoban::solve(Sokoban::Map&) (solver.cpp:19)
2716== by 0x40199F: main (main.cpp:28)
2716==
2716== 44,232 (7,344 direct, 36,888 indirect) bytes in 51 blocks are definitely lost in loss record 18 of 18
2716== at 0x4C2AF8E: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
2716== by 0x40815E: Sokoban::aStarPlayer(Sokoban::Node&, Sokoban::Point) ( aStarPlayer.cpp:145 )
==2716== by 0x40B563: Sokoban::canPlayerMoveBehind(Sokoban::Node*, int) (aStarDiamond.cpp:324)
2716== by 0x409C91: Sokoban::aStarDiamond(Sokoban::Node&, Sokoban::Point) (aStarDiamond.cpp:82)
2716== by 0x407392: Sokoban::solve(Sokoban::Map&) (solver.cpp:19)
2716== by 0x40199F: main (main.cpp:28)
2716==
2716== LEAK SUMMARY:
2716== definitely lost: 7,776 bytes in 54 blocks
2716== indirectly lost: 41,628 bytes in 1,471 blocks
2716== possibly lost: 0 bytes in 0 blocks
2716== still reachable: 0 bytes in 0 blocks
2716== suppressed: 0 bytes in 0 blocks
2716==
2716== For counts of detected and suppressed errors, rerun with: -v
2716== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 2 from 2)