Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i was trying to run my code but it isn't working at all and so much error
The Error is under the code

C++
  1  #include <iostream>
  2  #include <stack>
  3  #include <list>
  4  #include <queue>
  5  #include <algorithm>
  6  #include <iterator>
  7  #include <ctime>
  8  #include <vector>
  9  #include"state.h"
 10  using namespace std;
 11  
 12  int main(){
 13     srand(time(0));
 14     Game game;
 15     game.getIntBoard();
 16     game.playGame();
 17  
 18  
 19     return 0;
 20  }


C++
  1  <pre>#ifndef STATE_H_INCLUDED
  2  #define STATE_H_INCLUDED
  3  using namespace std;
  4  
  5  const int MAX_BOARD_INDEX = 2;
  6  const int MAX_STATES = 100;
  7  const int BOARDSIZE = 3;
  8  const int NUM_OF_BLOCKS = 6;
  9  
 10  class Action{
 11  private:
 12  int source;
 13  int destination;
 14  int heuristic;
 15  
 16  public:
 17      Action(){};
 18      Action(int s,int d):source(s),destination(d){};
 19  
 20      bool operator < (const Action a) const{
 21      if(heuristic < a.heuristic){
 22          return true;
 23          }else{
 24              return false;
 25              }
 26      }
 27      bool operator==(const Action a)const{
 28      if(source==a.source){
 29          return true;
 30          }else{
 31              return false;
 32              }
 33      }
 34  
 35      int getHeuristic(){return heuristic;}
 36  
 37      void setHeuristic(int h){heuristic = h;}
 38  
 39      void printAction()const{
 40      cout << "(" << source << "," << destination << ")" << endl;
 41      }
 42  
 43      void printActionInfo()const{
 44      cout << "(" << source << "," << destination << "),H:" << heuristic << endl;
 45      }
 46  
 47      void setAction(int s, int d){source = s; destination = d;}
 48  
 49      int getSource(){
 50      return source;
 51      }
 52  
 53      int getDestination(){
 54      return destination;
 55      }
 56  };
 57  
 58  class Board:public State{
 59  
 60  private:
 61      vector<int>blocks;
 62  
 63  public:
 64      void generateIntlState() {
 65          int bIndex = 0;
 66          intializeBlocks();
 67  
 68      for(int r = 0; r < BOARDSIZE; r++){
 69          for(int c = 0; c < BOARDSIZE; c++){
 70              setVal(r,c blocks[bIndex]);
 71              bIndex ++;
 72          }
 73      }
 74      pushBlockDown();
 75  
 76      void intializeBlocks(){
 77      int zeroCount = 1;
 78  
 79      for (int i = 1; i < 9; i ++){
 80          blocks.push_back(i);
 81          if(zeroCount<=3){
 82              blocks.push_back(0);
 83              }
 84              zeroCount++;
 85          }
 86      for( int i = 8; i > 0; i--){
 87          int num=rand()% i;
 88          int temp = blocks[i];
 89          blocks[i] = blocks[num];
 90          blocks[num] = temp;
 91          }
 92  }
 93  
 94      void findActHeuristic(priority_queue<Action>&actions, Goal g){
 95      Action a;
 96      vector<Action>actions_vec;
 97      vector<State> states;
 98      int h = 0;
 99  
100      for(int r = 0; r < BOARDSIZE; r++){
101          for(int c = 0; c < BOARDSIZE; c++){
102            if((r != c)&&(isFull(r))&&(isEmptu(c))){
103              a.setAction(r,c);
104              actions_vec.push_back(a);
105              }
106          }
107      }
108      for(int i = 0; i <actions_vec.size(); i++){
109          State newState = nextState(actions_vec[i]);
110          states.push_back(newState);
111          if(newState.getVal(g.getRow(),g.getColumn()) == g.getVal()){
112              h = 100;
113          }else if(newState.isOnTop(g.getVal())&&(newState.isColumnEmpty(g.getColumn()))){
114              h = 95;
115          }else if(newState.isOnTop(g.getVal())&&(newState.isBlockEmpty(g.getColumn()))){
116              h = 85;
117          }else if(newState.isBlockEmpty(g.getRow(),g.getColumn())){
118              h = 65;
119          }else if(newState.isColumnEmpty(g.getColumn()))&&(getRowPos(g.getVal()) > g.getRow()){
120              h = 55;
121          }else if(newState.isColumnEmpty(g.getColumn())){
122              h = 50;
123          }else if(newState.isOnTop(ggetVal())){
124              h = 40;
125          }else if(newState.isColumnFull(g.getColumn())){
126              h = 1;
127          }else{
128              h = 5;
129          }
130          actions_vec[i].setHeuristic(h);
131          actions.push(actions_vec[i]);
132      }
133  }
134  
135      template<typename T>void priority_queueQ(priority_queue<T>q){
136      while(!q.empty()){
137          q.top().printGoal();
138          q.pop();
139          }
140          cout << endl;
141      }
142  };
143  
144  class Game{
145  
146  private:
147  State state;
148  Board board;
149  SolverHD solverHD;
150  
151  public:
152      Game(){};
153  
154      void getIntBoard(){
155          board.generateIntlState();
156          cout <<  "Intial State:" << endl;
157          board.printBoard();
158      }
159  
160      void playGame(){
161          priority_queue<Goal>goals;
162          getUserGoal(goals);
163          solverHD.searchForGoal(board,goals);
164      }
165  
166      void getUserGoal(priority_queue<Goal>&goals){
167      bool added = false;
168      int v , r , c;
169      string enterMore;
170      vector<Goal>goal_vec;
171      vector<int>usedBlocks;
172      bool blockUsed = false;
173  
174      list<int> val = {1,2,3,4,5,6};
175      do{
176         cout << "Input a goal:" << endl;
177      do{
178          cout << "Block(1-6):";
179          cin >> v;
180      }while(v < 1 || v > 6);
181      do{
182          cout << "Row(0-2):";
183          cin >> r;
184      }while (r < 0 || r > 2);
185      do{
186          cout << "Column(0-2):";
187          cin >> c;
188      }while (c < 0 || c > 2);
189  
190      for(int i = 0; i < usedBlocks.size(); i++){
191          if(usedBlocks[i] == v){
192              cout << "\nA goal of this block has already bbeen added" << endl;
193              blockUsed = true;
194              break;
195          }else{
196          blockUsed = false;
197          }
198      }
199      usedBlocks.push_back(v);
200      val.remove(v);
201  
202      if(!blockUsed){
203          Goal g;
204          g.setGoal(v ,r, c);
205          goal_vec.push_back(g);
206          goals.push(g);
207          cout << "\nGoal (" << v << ", " << r << ", " << c << ") added\n";
208      }
209      cout << "\nPress y to enter another goal, otherwise press any key to quit" << endl;
210      cin >> enterMore;
211      while (enterMore == "y" || enterMore == "Y");
212  
213      while(!added){
214          int valToUse;
215          for(int i = 0; i < goal_vec.size(); i++){
216              if(goal_vec[i].getRow() > 0 ){
217                  for(int r = 0; r <goal_vec[i].getRow(); r++){
218                      valToUse = val.front();
219                      val.pop_front();
220  
221                      Goal g;
222                      g.setGoal(valToUse , r , goal_vec[i].getCol());
223                      goals.push(g);
224                  }
225              }
226          }
227         added = true;
228      }
229      cout << "Searching for the following conjunctive goals:\n"; board.printPriorityQ(goals);
230  };
231  
232  class Goal{
233  
234  private;
235  int val;
236  int row;
237  int col;
238  
239  public:
240      Goal(){};
241      Goal(int v , int r , int c){val = v; row = r; col = c;}
242  
243      bool operator<(const Goal g)const{
244          if(row > g.row ||(row == g.row&&col > g.col)||(row == g.row && g.col&&val > g.val)){
245              return true;
246          }else {
247              return false;
248          }
249      }
250  
251      void printGoal() const{
252          cout << "(" << val <<", " << row << ", " << col << ")" << endl;
253      }
254  
255      void setGoal(int v , int r , int c){
256      val = v; row = r; col = c;
257      }
258  
259      int getVal(){
260          return val;
261      }
262      int getRow(){
263          return row;
264      }
265      int getCol(){
266          return col;
267      }
268  };
269  
270  class SolverHD{
271  
272  private;
273  bool goalFound;
274  
275  public:
276      bool searchForGoal(Board b , priority_queue<Goal>&goals){
277      vector<State>usedStates;
278      vector<Goal>goalsAchieved;
279      int generatedStates = 0;
280      goalFound = false;
281  
282      usedStates.push_backk(b);
283  
284      while(generatedStates < MAX_STATE){
285       if(goals.size() == 0){
286          cout << "All goals have been satisfied!\n" << endl;
287          return false;
288       }
289       while(goals.size() > 0){
290          Goal g = goals.top();
291          if(b.getValue(g.getRow(),g.getCol()) == g.getVal()){
292              cout << "Goal(" << g.getVal() << "," << g.getRow() << "," << g.getCol() << ") was achieved\n" << endl;
293  
294          goalsAchieved.push_back(g);
295          goals.pop();
296          goalFound = true;
297          break;
298          }else{
299          goalFound = false;
300              }
301  
302          priority_queue<Action>actions;
303          b.findActHeuristic(actions, g);
304  
305          if(actions.size() == 0){
306              cout << "no actions available" << endl;
307              return false;
308          }
309          while(actions.size() > 0){
310              Action action = actions.top();
311              actions.pop();
312  
313              State newState = b.nextState(action);
314              bool flag = false;
315  
316              for(int i = 0; i < usedState.size(); i++){
317                  if(usedState[i] == newState){
318                      flag = true;
319                      break;
320                  }
321              }
322              if(flag){
323                  continue;
324                  }
325                  bool used = false;
326                  int topVal = b.getTopVal(action.getSource());
327                  for(int i = 0; i < goalsAchieved.size(); i++){
328                      if(goalsAchieved[i].getVal() == topVal){
329                          used = true;
330                      }
331                  }
332                  if(used){
333                      continue;
334                  }
335              b.moveBlock(action);
336              cout << "Step" << generatedStates+1 << ": Move Back" << "v" << ", from col" << action.getSource() << "to col" <<action.getHeuristic() << "." << endl;
337  
338              b.printBoard();
339              usedStates.push_back(b);
340              generatedState+++;
341              break;
342              }
343          }
344      }
345      if(!goalFound){
346          cout << "Max steps reached & Goal not found" << endl;
347      }
348      generatedStates = 0;
349      return true;
350      }
351      bool idFound(){
352      return goalFound;
353      }
354  };
355  
356  class State{
357  
358  private;
359  int grid[BOARDSIZE][BOARDSIZE];
360  
361  public:
362      State(){
363      for(int r = 0; r < BOARDSIZE; r++){
364          for(int c = 0; c < BOARDSIZE; c++){
365              grid[r][c] = 0;
366              }
367          }
368      }
369  
370      State(const State& s){
371      for(int r = 0; r < BOARDSIZE; r++){
372          for(int c = 0; c < BOARDSIZE; c++){
373              grid[r][c] = s.getValue(r,c);
374              }
375          }
376      }
377  
378      bool operator == (State s){
379      for(int r = 0; r < BOARDSIZE; r++){
380          for(int c = 0; c < BOARDSIZE; c ++){
381              if(grid[r][c] != s.getValue(r,c)){
382                  return false;
383                  }
384              }
385          }
386          return true;
387      }
388  
389      int getSize(){
390      return BOARDSIZE;
391      }
392  
393      int getNumberOfBlocks(){
394      return NUM_OF_BLOCKS;
395      }
396  
397      int getValue(int r, int c)const{
398      return grid[r][c];
399      }
400  
401      void setValue(int r, int c, int value){
402      grid[r][c] = value;
403      }
404  
405      void getPosition (int value, int& r, int& c);
406  
407      bool moveBlock(Action a){
408      if(isFull(a.getSource()) && isEmpty(a.getDestination())){
409         if(insertBlockTo(a.getDestination(), removeBlockFrom(a.getSource()))){
410          return true;
411              }
412          }
413          return false;
414      }
415  
416      State nextState(Action action){
417      State newState(*this);
418  
419      if(newState.moveBlockk(action))
420          return newState;
421  
422      return*this;
423      }
424  
425      void printBoard(){
426      cout << endl;
427      for(int r = 0; r < BOARDSIZE; r++){
428          cout << " |";
429          for(int c = 0; c < BOARDSIZE; c++){
430              if(grid[BOARDSIZE - r - 1][c] == 0){
431                  cout << "l"
432                  }else{
433                      cout << " " << grid[BOARDSIZE-r-1][c] << " |";
434                  }
435              }
436              cout << endl;
437          }
438          for(int c = 0; c < BOARDSIZE; c++){
439              cout << " ---";
440          }
441          cout << endl << endl;
442      }
443  
444      void pushBlocksDown(){
445      for(int c = 0; c < BOARDSIZE; c++){
446          queue<int>tempQueue;
447          for(int r = 0; r < BOARDSIZE; r++){
448              if(grid[r][c] != 0){
449                  tempQueue.push(grid[r][c]);
450                  }
451              }
452              for(int r = 0; r < BOARDSIZE; r++){
453                  if(!tempQueue.empty()){
454                      grid[[r][c] = tempQueue.front();
455                      tempQueue.pop();
456                  }else{
457                      grid[r][c] = 0;
458                  }
459              }
460          }
461      }
462  
463      bool isEmpty(int c){
464      for(int r = 0; r < BOARDSIZE; r++){
465          if(getValue(r,c) == 0 ){
466              return true;
467              }
468          }
469          return false;
470      }
471  
472      bool isFull(int c){
473      for(int r = 0; r < BOARDSIZE; r++){
474          if(getValeu(r,c) != 0){
475              return true;
476              }
477          }
478          return false;
479      }
480  
481      bool isColumnFull(int c){
482      return (get.Value(BOARDSIZE - 1,c) != 0);
483      }
484  
485      bool isColumnEmpty(int c){
486      if(getValue(0,c) == 0){
487          return true;
488          }
489          return false;
490      }
491  
492      bool isBlockEmpty(int r, int c){
493      if(getValue(r,c) == 0){
494          return true;
495          }
496          return false;
497      }
498  
499      bool isOnTop(int val){
500      for(int r = MAX_BOARD_INDEX; r > 0; r--){
501          for(int c = MAX_BOARD_INDEX; c > 0; c--){
502              if(getValue(r,c) == val){
503                  if(r < 2 && getValue(r+1,c) != 0){
504                      return false;
505                  }else{
506                  return true;
507                      }
508                  }
509              }
510          }
511          return false;
512      }
513  
514      int getTopVal(int c){
515      for(int r = MAX_BOARD_INDEX; r >= 0; r--)
516      if(grid[r][c] != 0){
517          return grid[r][c];
518          }
519          return -1;
520      }
521  
522      int getRowPos(int val){
523      for(int c = 0; c < BOARDSIZE; c++){
524          for(int r =0; r < BOARDSIZE; r++){
525              if(getValue(r,c) == val){
526                  return r;
527                  }
528              }
529          }
530          return -1;
531      }
532  
533      int removeBlockFrom(int c){
534      int holdBlock;
535  
536      for(int r = MAX_BOARD_INDEX; r >= 0; r--){
537          if(getValue(r,c) != 0){
538              holdBlock = getValue(r,c);
539              setValue(r,c,0);
540              return holdBlock;
541              }
542          }
543          return -1;
544      }
545  
546      bool insertBlockTo(int c, int value){
547      for(int r = 0; r <= MAX_BOARD_INDEX; r++){
548          if(getValue(r,c) == 0){
549              setValue(r,c,value);
550              return true;
551              }
552          }
553          return false;
554      }
555  
556      int getRow(int r){
557      return r;
558      }
559  
560      int getColumn(int c){
561      return c;
562      }
563  
564  };
565  
566  
567  #endif // STATE_H_INCLUDED


What I have tried:

||1\state.h|66|error: expected class-name before '{' token|
1\state.h|155|error: 'State' does not name a type|
1\state.h|156|error: field 'board' has incomplete type 'Board'|
1\state.h|66|note: forward declaration of 'class Board'|
1\state.h|157|error: 'SolverHD' does not name a type|
1\state.h|174|error: 'Goal' was not declared in this scope|
1\state.h|174|error: template argument 1 is invalid|
1\state.h|174|error: template argument 2 is invalid|
1\state.h|174|error: template argument 3 is invalid|
1\main.cpp|20|error: expected '}' at end of input|
1\main.cpp|20|error: expected unqualified-id at end of input|
1\main.cpp|20|error: expected '}' at end of input|
1\state.h||In member function 'void Board::generateIntlState()':|
1\state.h|74|error: 'intializeBlocks' was not declared in this scope|
1\state.h|78|error: expected ')' before 'blocks'|
1\state.h|78|error: 'setVal' was not declared in this scope|
1\state.h|82|error: 'pushBlockDown' was not declared in this scope|
1\state.h|84|error: a function-definition is not allowed here before '{' token|
1\state.h|150|error: expected '}' at end of input|
1\state.h||In member function 'void Board::Game::playGame()':|
1\state.h|169|error: 'Goal' was not declared in this scope|
1\state.h|169|error: template argument 1 is invalid|
1\state.h|169|error: template argument 2 is invalid|
1\state.h|169|error: template argument 3 is invalid|
1\state.h|171|error: 'solverHD' was not declared in this scope|
1\state.h||In member function 'void Board::Game::getUserGoal(int&)':|
1\state.h|178|error: 'Goal' was not declared in this scope|
1\state.h|178|error: template argument 1 is invalid|
1\state.h|178|error: template argument 2 is invalid|
1\state.h|198|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
1\state.h|211|error: expected ';' before 'g'|
1\state.h|212|error: 'g' was not declared in this scope|
1\state.h|213|error: request for member 'push_back' in 'goal_vec', which is of non-class type 'int'|
1\state.h|214|error: request for member 'push' in 'goals', which is of non-class type 'int'|
1\state.h|223|error: request for member 'size' in 'goal_vec', which is of non-class type 'int'|
1\state.h|224|error: invalid types 'int[int]' for array subscript|
1\state.h|225|error: invalid types 'int[int]' for array subscript|
1\state.h|229|error: expected ';' before 'g'|
1\state.h|230|error: 'g' was not declared in this scope|
1\state.h|230|error: invalid types 'int[int]' for array subscript|
1\state.h|231|error: request for member 'push' in 'goals', which is of non-class type 'int'|
1\state.h|238|error: expected 'while' before ';' token|
1\state.h|238|error: expected '(' before ';' token|
1\state.h|238|error: expected primary-expression before ';' token|
1\state.h|238|error: expected ')' before ';' token|
1\state.h|280|error: expected ':' before ';' token|
1\state.h|284|error: the value of 'Goal' is not usable in a constant expression|
1\state.h|178|note: 'Goal' was not declared 'constexpr'|
1\state.h|284|error: type/value mismatch at argument 1 in template parameter list for 'template<class _tp,="" class="" _sequence,="" _compare=""> class std::priority_queue'|
1\state.h|284|note:   expected a type, got 'Goal'|
1\state.h|284|error: template argument 2 is invalid|
1\state.h|284|error: template argument 3 is invalid|
1\state.h||In member function 'bool Board::Game::getUserGoal(int&)::SolverHD::searchForGoal(Board, int&)':|
1\state.h|285|error: 'State' was not declared in this scope|
1\state.h|285|error: template argument 1 is invalid|
1\state.h|285|error: template argument 2 is invalid|
1\state.h|286|error: use of local variable with automatic storage from containing function|
1\state.h|178|note: '<typeprefixerror>Goal' declared here|
1\state.h|286|error: use of local variable with automatic storage from containing f\
Posted
Updated 15-Apr-20 23:56pm
v3
Comments
Richard MacCutchan 16-Apr-20 5:02am    
There are nearly 600 lines of code there. You need to make an effort to correct the more obvious errors.
Richard MacCutchan 16-Apr-20 5:10am    
Your class definitions are out of order so the compiler is getting confused. You need to ensure that a base class is fully defined before you define a subclass that refers back to it.

1 solution

Those are all compilation errors, which means you typed a load of code - 600 lines - and then tried to compile it. And some of it you typed wrong! That's a bad idea, especially when you are starting out: type a function, compile and test it. Type another, compile and test that. Repeat.

That way, you don't end up with a huge wodge of syntax - and probably logic - errors and no idea what to do.

Those are all compiler errors, and as I told you last time you posted this, and the time before:
Quote:
Compile time errors occur when you try to compile your code and the compiler spits it out with file and line references - these are cause by your code using the wrong syntax (such as a missing close bracket perhaps), or spelling a variable differently in two places, or ... loads of reasons, all related to how your code was written.
These tend to be offered up to you as a list of problems with each having a message which describes what the compiler doesn't like, the file name it is in, and the line number it was found on.
Solution: edit the file, find the line (most editors will allow you to type CTRL+G then a lien number to "jump" to it) and read the line and a few above and below in conjunction with the message to see what the problem is. Repeat until you get a "clean compilation".


So do that.
||1\state.h|66|error: expected class-name before '{' token|
      ^      ^
      |      |
      |      \__  Line number
      |
      \_________  File name   

Edit state.h and find line 66. Look at the code of that line and a couple of lines above it. What is before the '{' character on line 66? What should it be?
Fix that one. Recompile.
Look at the first error message and repeat the above for eth new file and line.

We can't do that for you - and this is something you really, really, need to learn for yourself, so even if we could (which we can't, we don't have access to your files) it would be a bad idea for us to do!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900