include's extension

This is a discussion on include's extension within the C++ Programming forums, part of the General Programming Boards category; i'm having a little trouble grasping how C++ includes work. up till now ive had each of my classes in ...

  1. #1
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332

    include's extension

    i'm having a little trouble grasping how C++ includes work.

    up till now ive had each of my classes in one .h file which contains their declaration and implentation.

    i just read this was bad, fair enough, i cut and paste the implantaion code into a new .cpp file leaving only the class declaration in the .h file.

    now, how do i go about compiling this without it spewing out more errors than the computers can handle?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Depends on your compiler, but essentially you add all your class.cpp files to the project
    In VC.NET for example, you just right-click on the file and choose "add file to project"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332
    hrm. well i'm using dev-c++4.9, and it's basicly the same. my file structure is basicly this:

    -class1.h:
    contains declaration of class1
    -class1.cpp:
    implantation of class 1. "#includes" class1.h
    -class2.h
    declaration and implantation of class 2
    -class3.h
    declaration and implantation of class 3
    -main.cpp
    has regular includes, then inlcusion of class2.h and class3.h, tehn main function.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    You need to put macro guards in the .h files with the class declarations. Class functions are placed in the .cpp file. Your .cpp file should have #include "foo.h".


    Your .h file


    Code:
    #ifndef FOO_H
    #define FOO_H
    
    class foo
    {
       private:
          int num1, num2, num3;
    
       public:
         ....
         ....
         int calcNum (int, int);
    
    };
    #endif

    Your .cpp file


    Code:
    #include "foo.h"
    
    int foo::calcNum (int a, int b)
    {
       num3 = num1 + num2;
       return num3;
    }


    Code:
    #include "foo.h"
    
    int main ()
    {
        foo x;
        .....
        .....
        x.calcNum ();
        
       return 0;
    }

    NB: This does not apply to template classes. With templates, both the declaration and their operations must be within the .h file.

    Hope that helps.

    Sophie
    simple is always an understatement.....

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Posting code DOES help believe it or not. And the errors you're getting.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile and includes
    By Lang in forum C Programming
    Replies: 4
    Last Post: 03-18-2008, 03:29 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. remove(const char* ) only with extension?
    By mosu' in forum C++ Programming
    Replies: 4
    Last Post: 10-17-2006, 01:35 PM
  4. Installing Thumbnail shell extension ?
    By glennpierce in forum Windows Programming
    Replies: 0
    Last Post: 07-20-2006, 09:38 AM
  5. get file extension
    By Nada in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 08:12 AM

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