Problems compiling world's easiest program

This is a discussion on Problems compiling world's easiest program within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm having trouble compiling my program. main.cpp file: Code: #include <iostream> #include "Test.h" using namespace std; int main() { ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    20

    Problems compiling world's easiest program

    Hi, I'm having trouble compiling my program.

    main.cpp file:
    Code:
    #include <iostream>
    
    #include "Test.h"
    
    using namespace std;
    
    int main()
    {
       Test a;
       a.printSomething();
       cout << "Hello World" ;
       return 0;
    }
    Test.h file:
    Code:
    #ifndef TEST_H
    #define TEST_H
    
    class Test
    {
    public:
       void printSomething();
    };
    #endif
    Test.cpp file:
    Code:
    #include <iostream>
    #include "Test.h"
    
    using namespace std;
    
    void Test::printSomething();
    {
       cout << "Hi";
    }
    If I comment a.printSomething(); in my main.cpp file, I won't get compilation errors.

    But if I do, I get a "Undefined first referenced symbol in file" error

    Can anyone help?

    Okay, compiling with
    g++ main.cpp Test.cpp
    gives me no errors.

    But if I compile using
    g++ main.cpp Test.h
    I do get errors.

    Which is the correct way to compile?
    Last edited by markcls; 03-25-2007 at 10:27 PM.

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Code:
    #include "Test.h"
    should be after
    Code:
    using namespace std;
    Don't quote me on that... ...seriously

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Irrelevant. However, you have an extra semi-colon in test.cpp.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Which is the correct way to compile?
    g++ main.cpp Test.cpp

    Only compile your .cpp files. The .h files are included in them already and will be compiled automatically.

    >> #include "Test.h" should be after using namespace std;
    It's fine where it is. In fact I'd say it should not be after using namespace std.

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    >> It's fine where it is. In fact I'd say it should not be after using namespace std.
    On my compiler, it would have to be after using namespace std; but it's outdated so if it's incorrect, I'm sorry.
    Don't quote me on that... ...seriously

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> On my compiler, it would have to be after using namespace std; but it's outdated so if it's incorrect, I'm sorry.

    Are you sure? Even on an old compiler I've never heard of something like that. The only time you would need it to be after is if you needed the std namespace available in the header file and you did not do it yourself. However, the solution to that would be to add the using directive in the header or specify std:: directly in there.

  7. #7
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    >>Are you sure? Even on an old compiler I've never heard of something like that. The only time you would need it to be after is if you needed the std namespace available in the header file and you did not do it yourself. However, the solution to that would be to add the using directive in the header or specify std:: directly in there.

    Yeah. That's what it was. It's too late to be looking at code.
    Don't quote me on that... ...seriously

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    20
    Quote Originally Posted by Desolation
    Irrelevant. However, you have an extra semi-colon in test.cpp.
    Ouch, didn't see that.

    Anyways thanks guys! Do you guys even ever sleep?

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by markcls
    Do you guys even ever sleep?
    I thought to be a coder you had to give up sleep.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by MacGyver
    I thought to be a coder you had to give up sleep.
    What's that thing again ? Can I buy that on eBay ?

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by Desolation
    What's that thing again ? Can I buy that on eBay ?
    LOL. Sorry to go off topic, but that reminds me so much of this hillarious Sid Caesar sketch I saw where they're doing a parody of an old movie.

    Sid plays this old time 1920's style gangster that falls in love with woman who hates him and tells him she won't marry him because he's "uncouth". He turns to his goons, pulls out a load of money and says to them, "Go to the store and buy me as much couth as they got."

    That still cracks me up.

    OK, sorry, back to coding.

  12. #12
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I thought that is what they put into Jolt... (sleep that is, not couth-ness)

  13. #13
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by markcls
    Ouch, didn't see that.

    Anyways thanks guys! Do you guys even ever sleep?
    We strategically placed ourselves in different timezones to provide 24 hour support.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Needed with Compiling C program
    By girishaneja in forum C Programming
    Replies: 2
    Last Post: 05-20-2009, 09:10 AM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  3. Few problems with my program
    By kzar in forum C Programming
    Replies: 6
    Last Post: 06-22-2005, 08:58 AM
  4. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 11:45 AM

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