• Advertisement

Archived

This topic is now archived and is closed to further replies.

this is weird....

This topic is 5992 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

heres a weird problem ive never seen before i have a Monster.hpp file that defines a class but doesnt impliment the functions and i have a Monster.cpp file that defines all the functions and such now i include the Monster.cpp file in the main cpp file( which is main) and in the Monster.cpp file i include Monster.hpp but i get some weird errors like this: 16 monster.cpp implicit declaration of function `int rand(...)'' 52 monster.cpp `cin'' undeclared (first use this function) 62 monster.cpp `cout'' undeclared (first use this function) these errors arise in Monster.cpp can anyone tell me whats wrong plz?

Share this post


Link to post
Share on other sites
Advertisement
First of all, don't include a .cpp file into another .cpp file!
The proper way of doing what you're (probably) trying to do, is to compile each .cpp file separately and then *link* them together (Read the docs for your compiler and linker if you don't understand what I'm talking about).

Now, to your problem: Most likely you haven't included the correct header-files.

'cin' and 'cout' are defined in "iostream.h" (or "iostream").
The 'rand' function is defined in "stdlib.h" (or "cstdlib").

[EDIT: Spelling mistakes]

Edited by - Dactylos on September 22, 2001 8:01:51 PM

Share this post


Link to post
Share on other sites
i have all the correct files in the main file
im not stupid enough to forget them

i am on the other hand stupid enough to include a cpp in another cpp


time to read some docs

Share this post


Link to post
Share on other sites
Make the hpp file a .h file instead.

I dont think that makes a difference but it is the .. right(?) way to do it.

"I''ve sparred with creatures from the nine hells themselves... I barely plan on breaking a sweat here, today."~Drizzt Do''Urden

Share this post


Link to post
Share on other sites
but if im making a C++ project shouldn''t I make a hpp file instead of h?


the help file that comes with Dev C++( the ide I use) doesnt contain a very umm good docs

maybe i didnt look hard enough

if anyone has Dev C++ and has encountered this problem can you plz enlighten me

Share this post


Link to post
Share on other sites
Perhaps you included the headers in the wrong order (i.e. including Monster.cpp first, and then the stdlib.h and iostream.h headers? (Of course then the problem would go away if you no longer include Monster.cpp in main.cpp)

(If I am pointing out the obvious, or seem patronizing, it's only because I don't know what level you're on )

Giving C++ headers a .hpp extension is perfectly valid/standard.

usual C++ file extensions:
.C     (only usable on systems where case matters in filenames)
.cc
.cpp
.cxx

usual C++ header file extensions:
.h
.hpp
.hxx

Edited by - Dactylos on September 22, 2001 8:01:30 PM

Share this post


Link to post
Share on other sites
If you did ''#include <iostream>'' rather than ''#include <iostream.h>'', you''ll need to resolve the namespace (cout and cin are now part of the std namespace):

#include
using namespace std;
cout << "This sucka''ll work" << endl;

OR

#include
std::cout << "This works too." << endl;

or, alternatively

#include
cout << "This uses the older headers for backwards compatibility. Start moving to the new method (above)." << endl;

Share this post


Link to post
Share on other sites
im not that big of a newb to not know these things

ive done everything right in terms of header files and their order


these things have never happened in the +1 years ive used Dev C++

im trying to do wat dac said and find out how to link the dam cpp files together but i cant find anything in the help files

Share this post


Link to post
Share on other sites
Dev-C++ is an IDE, right? What compiler are you using. I''ll assume for a moment that it''s gcc/DJGPP. If that''s the case, I believe the link command is ''link''. However, the ''gcc'' command automatically handles that step if a bunch of .cpp and/or .o files are passed to it with the appropriate parameter, and you IDE probably already resolves. What you should look into is how to create projects/project workspaces with Dev-C++, so the IDE can know to link all the files together.

I think. That''s what I gather from your reply.

Share this post


Link to post
Share on other sites
uhh i do create projects

i do and did everything required to get this damn thing working and it starts to hate me

Share this post


Link to post
Share on other sites
Maybe Dev C++ doesnt like you. Try MSVC++, or Code Warrior, either one is better than Dev C++ (or MingW... whatever you call it).

Try taking whats in the header file and putting it in the .cpp file and see if it works then.

"I''ve sparred with creatures from the nine hells themselves... I barely plan on breaking a sweat here, today."~Drizzt Do''Urden

Share this post


Link to post
Share on other sites
how bout i just put the whole thing in the header file?

i could have done that in the first place and saved us all a great deal of trouble but i wanted it all in seperate files

oh well

Share this post


Link to post
Share on other sites
One of my text games, I tried to make a file "rooms.cpp" that I included into my main .cpp and boy did my compiler moan, groan, and throw errors left and right. Eventually, I just remade the project and changed room.cpp to room.h and boom, worked.

Your extentions *MAY* be the problem, though I''m not promising anything, that was the problem in my case


*ahem*
~All your base are belong to us~

Share this post


Link to post
Share on other sites
at the moment i dont give a dung
i just did what you did

i just crammed the whole class functions and all into one header file

it went from like 31 errors to 0

Share this post


Link to post
Share on other sites

  • Advertisement