Organisting code files basics.

This is a discussion on Organisting code files basics. within the C++ Programming forums, part of the General Programming Boards category; Hi there, Up until now I've only ever used a main.cpp file and a class.h header file, but am trying ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Organisting code files basics.

    Hi there,

    Up until now I've only ever used a main.cpp file and a class.h header file, but am trying to improve my separating of class interface and implementation.

    I have the following problem though:

    fatal error C1014: too many include files : depth = 1024
    fatal error C1014: too many include files : depth = 1024
    Here is some example code of my program:

    Header file (the class interface) 'Base.h' :

    Code:
    #include "Base.cpp"
    class Base
    {
    public:
    
    	Base(int x);
    
    private:
    	int x_;
    };
    Class implementation 'Base.cpp' :

    Code:
    #include "Base.h"
    Base::Base(int x)
    {
    	val_ = x;
    }
    main.cpp :

    Code:
    #include <iostream>
    #include "Base.h"
    using namespace std;
    
    int main()
    {
    	Base b1(2);
    
    	system ("PAUSE");
    	return 0;
    }
    I understand that using:

    Code:
    #ifndef FILENAME.H
    #define FILENAME.H
      //Header file code here
    #endif
    Is often a way to solve this problem, but unless I'm mistaken, this is not required for my program.

    The problem is me not understanding how to correctly include the files amongst one another.

    So I was hoping someone could tell me my error so that I get this all correct, ie: which #includes are required and which are not.

    Many thanks!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Nothing should ever include a .cpp file. In this case, it is producing an infinite include loop. Base.cpp includes Base.h which includes Base.cpp which include Base.h which includes...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks brewbuck!

    I seem to have it working now, this is what I did:

    Header file (the class interface) 'Base.h' :

    Code:
    class Base
    {
    public:
    
    	Base(int x = 0);
    
    private:
    	int x_;
    };
    Class implementation 'Base.cpp' :

    Code:
    #include "Base.h"
    Base::Base(int x)
    {
    	x_ = x;
    }
    main.cpp :

    Code:
    #include <iostream>
    #include "Base.h"
    using namespace std;
    
    int main()
    {
    	Base b1(2);
    
    	system ("PAUSE");
    	return 0;
    }
    Hope this looks OK, really appreciate your help with this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Object Oriented and Multiple Files Basics?
    By markcls in forum C++ Programming
    Replies: 4
    Last Post: 03-25-2007, 12:21 PM
  2. Large exe files for small amounts of code problem
    By rainmanddw in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2003, 04:52 PM
  3. Need a Code to display .jpg files
    By arvindkr in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2002, 05:43 PM
  4. Replies: 0
    Last Post: 02-21-2002, 05:05 PM
  5. header files and code files..
    By CompiledMonkey in forum C++ Programming
    Replies: 4
    Last Post: 02-15-2002, 08:35 AM

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