|
-
Aug 20th, 2001, 12:46 AM
#1
Thread Starter
Lively Member
Class in a h file plz help
Hi All,
I'm having a bit of trouble linking my header file that has my class definitions in with my cpp method file which has all of the methods. My compiler keeps giving me errors like as if the definitions don't exist. Here's some sample code below...
classes.h
<code>
class elevator //define elevator class
{
int level;
int peoplein;
public:
void initialize(void);
void go_up(void);
void go_down(void);
void next_floor(void);
void open_doors(void);
void close_doors(void);
};
class building //define building class (manager)
{
elevator elevator1;
person person1;
public:
bool closed;
void initialize(void);
};
</code>
and in my methods cpp...
meth.cpp
<code>
void elevator::initialize(void)
{
level = 1;
peoplein = 0;
cout << "Floor " << level << "..." << endl;
}
void elevator: pen_doors(void)
{
//Check if people need to come aboard, if so aboard them.
}
void elevator::close_doors(void)
{
char key;
cout << "Hit a key to close the lift doors...(Q to quit)"<< endl;
cin >> key;
if(key = 'q')
elevator.closed = 1;
}
void elevator::next_floor(void)
{
if (level ==20)
elevator::go_down();
else
elevator::go_up();
}
void elevator::go_up(void)
{
cout << "Going up..." << endl;
level++
cout << "Floor " << level << endl;
}
void elevator::go_down(void)
{
cout << "Going down..." << endl;
level--
cout << "Floor " << level << endl;
}
</code>
The compiler errors basically say that when compiling the h, meth.cpp and the main.cpp file (which has the inclusion statment to the h file) that it doesn't know any of the variables in meth.cpp such as level, elevator.closed etc.
Any Help would greatly be appreciated!
Thanks.
Best Regards,
Smithy.
-
Aug 20th, 2001, 04:04 AM
#2
Monday Morning Lunatic
Add #include "meth.h" to the top of meth.cpp
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 20th, 2001, 04:15 AM
#3
I tried to compile your code but I was missing some classes. Anyway, you must #include the header file in both main.cpp and meth.cpp.
In meth.cpp, in the close_doors proc:
PHP Code:
void elevator::close_doors(void)
{
char key;
cout << "Hit a key to close the lift doors...(Q to quit)"<< endl;
cin >> key;
if(key = 'q')
elevator.closed = 1;
}
I think it should be changed to:
PHP Code:
void elevator::close_doors(void)
{
char key;
cout << "Hit a key to close the lift doors...(Q to quit)"<< endl;
cin >> key;
if (key == 'q')
this->closed = 1;
}
This makes more sense but still there is no member variable named closed.
Anyway, I hope I helped.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|