declaration problem.

This is a discussion on declaration problem. within the C++ Programming forums, part of the General Programming Boards category; 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!"; } ...

  1. #1
    Unregistered
    Guest

    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.

  2. #2
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    make a header file for T.cpp and include it in main.cpp
    Please excuse my poor english...

  3. #3
    Unregistered
    Guest
    NO I don`t want to do header file.
    I know that it must work somehow.

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Well, if you don't include T.cpp in there somehow, how do you expect the compiler to know what TEXT is?

  5. #5
    Unregistered
    Guest
    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?

  6. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    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?

  7. #7
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    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
    Monday - what a way to spend a seventh of your life

  8. #8
    Unregistered
    Guest
    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.

  9. #9
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    You can compile them separately, but for main.cpp to work right, you'll still have to include t.cpp.

  10. #10
    Unregistered
    Guest
    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?

  11. #11
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    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.
    Monday - what a way to spend a seventh of your life

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    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.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  13. #13
    Unregistered
    Guest
    >"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.

  14. #14
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >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; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. yet another forward declaration problem.
    By sloppy_coder in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2005, 01:59 AM
  3. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21