| | |
something wrong with class!!
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 8
Reputation:
Solved Threads: 0
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
the error comes up in class hero...function jumpright
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <conio.h> #include <afx.h> #define rows 48 #define cols 80 class screen { private: char x[rows][cols]; public: screen() { int i,j; for(i=0;i<rows;i++) { for(j=0;j<cols;j++) { x[i][j]=' '; } } for(i=0;i<cols;i++) { x[0][i]='#'; } for(i=0;i<cols;i++) { x[rows-1][i]='#'; } } void setcell(int i,int c,char t) { x[i][c]=t; } void show() { system("cls"); for(int i=0;i<rows;i++) { for(int j=0;j<cols;j++) { cout<<x[i][j]; } } cout.flush(); } }; class hero { private: int posr; int posc; int health; int direction; int speed; public: hero() { posr=rows-4; posc=3; health =5; direction =1; speed =0; } void drawhero(screen &x) { char t=1; { x.setcell(posr,posc,t); x.setcell(posr+1,posc,'|'); x.setcell(posr+1,posc-1,'-'); x.setcell(posr+1,posc+1,' -'); x.setcell(posr+2,posc-1,'|'); x.setcell(posr+2,posc+1,'|'); } } void delhero(screen &x) { { x.setcell(posr,posc,' '); x.setcell(posr+1,posc,' '); x.setcell(posr+1,posc-1,' '); x.setcell(posr+1,posc+1,' '); x.setcell(posr+2,posc-1,' '); x.setcell(posr+2,posc+1,' '); } } void moveright(screen &x) { if(posc<cols-2) { delhero(x); posc++; drawhero(x); } } void moveleft(screen &x) { if(posc>1) { delhero(x); posc--; drawhero(x); } } void hit() { health--; } int gethealth() { return health; } void jumpright(screen &x, game &t) { delhero(x); posc+=2; posr--; drawhero(x); automove(); Sleep(100); show(); delhero(x); posc++; drawhero(x); automove(); Sleep(50); show(); delhero(x); posc+=2; posr++; drawhero(x); automove(); } }; class game { screen x; hero bob; public: game() { int t; t= bob.gethealth(); cout<<t; } void automove() { cout<<"teet"<<endl; } void usermove(char t,game &nagy) { if(t=='a') bob.moveleft(x); if(t=='d') bob.moveright(x); if(t=='e') bob.jumpright(x); } void show() { bob.drawhero(x); x.show(); } int run() { return 1; } }; void main() { char t; game nagy; nagy.show(); while (nagy.run()) { t=getch(); nagy.usermove(t,nagy); nagy.show(); } }
Last edited by Ancient Dragon; Jan 29th, 2009 at 9:36 am. Reason: add code tags
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
-chandra
•
•
Join Date: Jan 2009
Posts: 8
Reputation:
Solved Threads: 0
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???
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.
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:
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)
Now you can put the defenition in the cpp file:
dani.cpp:
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
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)
class foo { public: foo(); ~foo(); void func(void); }; class bar { public: bar(); ~bar(); void func2(void); };
Now you can put the defenition in the cpp file:
dani.cpp:
C++ Syntax (Toggle Plain Text)
#include "dani.h" void bar::func2() { foo * f = new foo(); //foo is declared delete f; } void foo::func() { bar * b = new bar(); // bar is in declared delete b; }
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.
![]() |
Similar Threads
- help! what's wrong with this class & it's functions/calls? (C++)
- java Calculator- wrong calculation (Java)
- What am I doing wrong? (C)
- Whats wrong with this class??? (C++)
- What's wrong with this class declaration? (C)
- problems accessing class instances (Java)
Other Threads in the C++ Forum
- Previous Thread: need help in performance of algorithms.
- Next Thread: Void Array Sorting/Swapping
| Thread Tools | Search this Thread |
api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib library linkedlist linker linux list loop looping loops map math matrix memory newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg simple string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






