943,733 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 403
  • C++ RSS
Jan 29th, 2009
0

something wrong with class!!

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kadamora is offline Offline
10 posts
since Jan 2009
Jan 29th, 2009
1

Re: something wrong with class!!

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.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jan 29th, 2009
0

Re: something wrong with class!!

In addition to Agni's post: You should also post the errormessages you get.
Last edited by Nick Evan; Jan 29th, 2009 at 8:11 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jan 29th, 2009
0

Re: something wrong with class!!

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kadamora is offline Offline
10 posts
since Jan 2009
Jan 29th, 2009
0

Re: something wrong with class!!

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)

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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 Nick Evan; Jan 29th, 2009 at 10:38 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: need help in performance of algorithms.
Next Thread in C++ Forum Timeline: Void Array Sorting/Swapping





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC