something wrong with class!!

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2009
Posts: 8
Reputation: kadamora is an unknown quantity at this point 
Solved Threads: 0
kadamora kadamora is offline Offline
Newbie Poster

something wrong with class!!

 
0
  #1
Jan 29th, 2009
i'm doing a game for my c++ course and there is something wrong with a class

the error comes up in class hero...function jumpright
  1. #include <iostream.h>
  2. #include <conio.h>
  3. #include <afx.h>
  4. #define rows 48
  5. #define cols 80
  6.  
  7.  
  8.  
  9. class screen
  10. {
  11. private:
  12. char x[rows][cols];
  13.  
  14. public:
  15.  
  16.  
  17. screen()
  18. {
  19. int i,j;
  20. for(i=0;i<rows;i++)
  21. {
  22. for(j=0;j<cols;j++)
  23. {
  24. x[i][j]=' ';
  25. }
  26. }
  27.  
  28.  
  29. for(i=0;i<cols;i++)
  30. {
  31. x[0][i]='#';
  32. }
  33.  
  34.  
  35. for(i=0;i<cols;i++)
  36. {
  37. x[rows-1][i]='#';
  38. }
  39.  
  40.  
  41.  
  42.  
  43. }
  44.  
  45.  
  46. void setcell(int i,int c,char t)
  47. {
  48. x[i][c]=t;
  49. }
  50.  
  51. void show()
  52. {
  53. system("cls");
  54. for(int i=0;i<rows;i++)
  55. {
  56. for(int j=0;j<cols;j++)
  57. {
  58. cout<<x[i][j];
  59. }
  60. }
  61. cout.flush();
  62. }
  63.  
  64.  
  65. };
  66.  
  67.  
  68.  
  69.  
  70. class hero
  71. {
  72. private:
  73.  
  74. int posr;
  75. int posc;
  76. int health;
  77. int direction;
  78. int speed;
  79.  
  80.  
  81. public:
  82.  
  83. hero()
  84. {
  85. posr=rows-4;
  86. posc=3;
  87. health =5;
  88. direction =1;
  89. speed =0;
  90. }
  91.  
  92.  
  93.  
  94. void drawhero(screen &x)
  95. {
  96. char t=1;
  97.  
  98.  
  99. {
  100.  
  101.  
  102.  
  103. x.setcell(posr,posc,t);
  104. x.setcell(posr+1,posc,'|');
  105.  
  106. x.setcell(posr+1,posc-1,'-');
  107. x.setcell(posr+1,posc+1,' -');
  108.  
  109. x.setcell(posr+2,posc-1,'|');
  110. x.setcell(posr+2,posc+1,'|');
  111. }
  112. }
  113.  
  114.  
  115.  
  116.  
  117. void delhero(screen &x)
  118. {
  119.  
  120.  
  121.  
  122.  
  123. {
  124.  
  125. x.setcell(posr,posc,' ');
  126. x.setcell(posr+1,posc,' ');
  127.  
  128. x.setcell(posr+1,posc-1,' ');
  129. x.setcell(posr+1,posc+1,' ');
  130.  
  131. x.setcell(posr+2,posc-1,' ');
  132. x.setcell(posr+2,posc+1,' ');
  133. }
  134. }
  135.  
  136.  
  137.  
  138.  
  139. void moveright(screen &x)
  140. {
  141.  
  142. if(posc<cols-2)
  143. {
  144.  
  145. delhero(x);
  146.  
  147. posc++;
  148.  
  149. drawhero(x);
  150. }
  151.  
  152. }
  153.  
  154.  
  155.  
  156. void moveleft(screen &x)
  157. {
  158. if(posc>1)
  159. {
  160.  
  161. delhero(x);
  162. posc--;
  163. drawhero(x);
  164.  
  165. }
  166. }
  167.  
  168. void hit()
  169. {
  170. health--;
  171. }
  172.  
  173. int gethealth()
  174. {
  175. return health;
  176. }
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184. void jumpright(screen &x, game &t)
  185. {
  186. delhero(x);
  187. posc+=2;
  188. posr--;
  189. drawhero(x);
  190. automove();
  191. Sleep(100);
  192. show();
  193.  
  194.  
  195.  
  196. delhero(x);
  197. posc++;
  198. drawhero(x);
  199.  
  200. automove();
  201. Sleep(50);
  202. show();
  203.  
  204.  
  205.  
  206. delhero(x);
  207. posc+=2;
  208. posr++;
  209. drawhero(x);
  210.  
  211.  
  212. automove();
  213.  
  214.  
  215.  
  216. }
  217.  
  218.  
  219. };
  220.  
  221.  
  222.  
  223.  
  224.  
  225. class game
  226. {
  227.  
  228. screen x;
  229. hero bob;
  230.  
  231. public:
  232.  
  233. game()
  234. {
  235. int t;
  236. t= bob.gethealth();
  237. cout<<t;
  238. }
  239.  
  240.  
  241. void automove()
  242. {
  243.  
  244. cout<<"teet"<<endl;
  245. }
  246.  
  247. void usermove(char t,game &nagy)
  248. {
  249. if(t=='a')
  250. bob.moveleft(x);
  251.  
  252. if(t=='d')
  253. bob.moveright(x);
  254.  
  255. if(t=='e')
  256. bob.jumpright(x);
  257.  
  258.  
  259. }
  260.  
  261. void show()
  262. {
  263.  
  264.  
  265.  
  266. bob.drawhero(x);
  267. x.show();
  268. }
  269.  
  270.  
  271. int run()
  272. {
  273. return 1;
  274. }
  275.  
  276. };
  277.  
  278.  
  279. void main()
  280. {
  281.  
  282. char t;
  283. game nagy;
  284. nagy.show();
  285.  
  286. while (nagy.run())
  287. {
  288.  
  289. t=getch();
  290. nagy.usermove(t,nagy);
  291.  
  292.  
  293.  
  294. nagy.show();
  295. }
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302. }
Last edited by Ancient Dragon; Jan 29th, 2009 at 9:36 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 449
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: something wrong with class!!

 
1
  #2
Jan 29th, 2009
There's a post at the top of the forum Read This before posting. It talks about using code tags and posting well indented and easy to read code. Please go through it.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,921
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 304
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: something wrong with class!!

 
0
  #3
Jan 29th, 2009
In addition to Agni's post: You should also post the errormessages you get.
Last edited by niek_e; Jan 29th, 2009 at 8:11 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 8
Reputation: kadamora is an unknown quantity at this point 
Solved Threads: 0
kadamora kadamora is offline Offline
Newbie Poster

Re: something wrong with class!!

 
0
  #4
Jan 29th, 2009
ok sorry
i use vc++ 06
the error is
error C2061: syntax error : identifier 'game'
and i posted the whole code cause i think it is the arrangement .

i have class game down...so i don't know y it doesn't accept it...

so do i have to define the class in the beginning like functions???
Last edited by kadamora; Jan 29th, 2009 at 9:43 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,921
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 304
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: something wrong with class!!

 
0
  #5
Jan 29th, 2009
Try reformatting your code, it's damn hard to read with all those white lines in it...

anyway, the error you posted is probably due to this line: void jumpright(screen &x, game &t) Here you tell the function "jumpright" that it can expect a class 'game', but the problem is that the class 'game' is not yet defined. You define the class a few lines later.

There are a number of horrible hacks to makes this code compile, but what I would really recommend is that you learn how to use header files.

Put your declaration in the header file like this (dani.h)

  1. class foo
  2. {
  3. public:
  4. foo();
  5. ~foo();
  6. void func(void);
  7. };
  8.  
  9. class bar
  10. {
  11. public:
  12. bar();
  13. ~bar();
  14. void func2(void);
  15. };

Now you can put the defenition in the cpp file:

dani.cpp:

  1.  
  2. #include "dani.h"
  3.  
  4. void bar::func2()
  5. {
  6. foo * f = new foo();
  7. //foo is declared
  8. delete f;
  9. }
  10.  
  11. void foo::func()
  12. {
  13. bar * b = new bar();
  14. // bar is in declared
  15. delete b;
  16. }

Don't forget to make a body for constructer/destructors to !

also read this about using void main()

And you also might want to update your compiler (turbo) to something from the last 10 years... Visual studio 2008 express is free for example
Last edited by niek_e; Jan 29th, 2009 at 10:38 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC