Thread: Classes with seperate .h and .cpp file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25

    Classes with seperate .h and .cpp file

    File: Test.cpp
    Code:
    #include <iostream>
    using namespace std;
    
    Test()
    {
          name = "Abc";
         cout << name;
    }
    Headerfile:
    Code:
    #ifndef TEST_H
    #define TEST_H
    
    class Test                   
    {
    protected:
        char* name;                
    
    public:
        Test();
    };
    
    #include "..\Test.cpp"  //Path is correct
    
    #endif
    My Main
    Code:
    #include <iostream>
    #include "..\HeaderFiles\Test.h" //Path is correct.
    
    int main()
    {
    cout << "Hello World"; // :D
    Test t = new Test();
    
    return 0;
    }
    If I put everything in one file (one header) it works, to then include this into the main.cpp:
    Code:
    #ifndef GREN_H
    #define GREN_H
    
    class Test
    {
    protected:
        char* name;
    public:
        Test();
    };
    
    Test::Test()
    {
    
    }
    #endif

    Edit:
    I got this to work! But:
    Why can I not do it the way I want to: putting the .cpp file into the header, instead of putting the header into the .cpp file. Am I doing something wrong, or this cannot be done depending on the compiler ("Thats how the compiler is built")?

    Writing the top then import the bottom, or writing the bottom to later import the top, should not make a difference?

    I find this way more logical for me, so really wanna pull this off!
    Last edited by ManyTimes; 04-08-2010 at 05:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting template classes between .h and .cpp files
    By Neo1 in forum C++ Programming
    Replies: 12
    Last Post: 01-16-2010, 05:47 AM
  2. extern Global array in seperate .h file
    By rocketman03 in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2009, 06:03 AM
  3. Using classes in another .cpp file
    By ryeguy in forum C++ Programming
    Replies: 13
    Last Post: 12-30-2007, 11:50 AM
  4. placement of (.h) and (.cpp) files..!?
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 06:37 PM
  5. Replies: 4
    Last Post: 05-28-2002, 04:14 AM