-
declaration problem.
I have two files main.cpp and T.cpp
T.cpp cosists following
#include <iostream.h>
class TEXT{
public:
void g(void){
cout<<"Hello World!";
}
};
and main.cpp
#include <iostream.h>
TEXT text; // I think that is mistake
int main(void){
text.g();
return 0;
}
Why dosn't this program work? I have used
extern TEXT text;
But this doasn`t work also.
What is the problem.
-
make a header file for T.cpp and include it in main.cpp
-
NO I don`t want to do header file.
I know that it must work somehow.
-
Well, if you don't include T.cpp in there somehow, how do you expect the compiler to know what TEXT is?
-
This is exactly what I want to know,
how the compiler know what is TEXT.
When the TEXT is functoin I can use it when I write exter void TEXT(void);
to the main.cpp fail.
Why doasn`t this work with class?
-
Well, in a properly written program, it would know it was TEXT because it's in a header. You're already including iostream, why's it such a big deal to include 1 more?
-
that is ridiculous, you have to include it as a header, what you are saying is the same as
im not going to include <iostream.h> or <conio.h>
and i want to use cout and getch() why cant i?
include the header, and it will work
-
Yes it work when I include T.cpp as header file. It add this header file to the beginning of the main.cpp. But I want to compile these two files separatly.
It doasn`t matter when the program is small just with few classis, but it is very useful when the program get bigger.
sorry when something is unclear for you my english isn`t very good.
-
You can compile them separately, but for main.cpp to work right, you'll still have to include t.cpp.
-
no I haven`t to include T.cpp
this is how it works when TEXT is function
//T.cpp
#include <iostream.h>
void TEXT (void){
cout<<"Hello World!";
}
//main.cpp
#include <iostream.h>
extern void TEXT(void); // this is how it worj with function
int main(void){
TEXT();
return 0;
}
But when there are TEXT class how it work then?
-
I feel this may go on for some time
So
If you define something in a header file (whether it is a function, struct, class, const - whatever) if you want to use or reference that in an application without explicitly defining withn the code you MUST include the header file that includes that definition.
this is a statement of fact, no matter how much you insist otherwise.
-
Whoever you are, how about you go and actually LOCATE your brain and THINK about things before you post... otherwise you'll continually post ridiculous stuff like this!!!
geeez.. i swear i've had my fill of people like you.
"I want to code in a language, and i'm not following the rules of the language's structure and compilation process... WHY DOESN'T IT WORK??"
i think you should pack your computer up and return it to the people that you bought it off... tell them that you are TOO STUPID TO OWN A COMPUTER!!!
kind regards :)
U.
-
>"I want to code in a language, and i'm not following the rules of the language's structure and compilation process... WHY DOESN'T IT WORK??"
Because he's 'learning'. You know, that thing you did when you had no knowledge of programming. So far all I've seen you do is insult people for lack of experience and I find it revolting. If all you can do is flame, then go hang out on an IRC chat room and leave these boards to those who want to learn and those who want to help.
>geeez.. i swear i've had my fill of people like you.
Then do us all a favor and leave.
-
>But when there are TEXT class how it work then?
When you've externed the function you've given a declaration. As has already been pointed out this would normally be in a header file, but if you really want to do it without a header then something like this might work -
T.cpp -
Code:
#include <iostream.h>
class TEXT{
public:
void g(void);
};
void TEXT::g(void){cout<<"Hello World!";}
TEXT text;
main.cpp -
Code:
#include <iostream.h>
extern class TEXT{
public:
void g(void);
}text;
int main(void){
text.g();
return 0;
}