Thread: Preventing an Unused Function from Compiling

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    Preventing an Unused Function from Compiling

    Let's say I have the following program called test.cpp:
    Code:
    #include <iostream>
    #include "my_header.h"
    
    using namespace std;
    
    
    void swap(int &a, int &b);
    
    		
    int main()
    {
    	int a, b;
    	
    	a = 1;
    	b = 2;
    		
    	swap( a, b);
    
    	cout <<"After calling swap:"<< endl;
    	cout << "a = " << a << endl;
    	cout << "b = " << b << endl;
    	
    	return 0;
    }
    It calls a function called swap, which is defined in the file my_header.h.

    This is all dandy, but the file my_header.h defines another function called square. This is shown below:
    Code:
    void swap(int &a, int &b)
    {
    	int temp;
    	
    	temp = a;
    	a = b;
    	b = temp;
    }
    
    void square(int &a, int &b)
    {
    	int temp;
    	
    	square_var(a, b, temp);
    }
    The function square calls another function, square_var, which is defined in a header file called my_other_header.h. Let's say that I want to compile the main program on my friends computer. Is there a way I can modify my_header.h so that my friend can compile test.cpp on his computer with out having to have the file my_other_header.h??

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    630
    You shouldnt put the whole functions in headers, only prototypes.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Further to l2u's comment, any code that you would place in a header file should be placed in a connecting .cpp file like so:

    example.h

    Code:
    #ifndef EXAMPLE_H
    #define EXAMPLE_H
    class MyClass
    {
    public:
       MyClass();
       ~MyClass();
    
       void swap ( int, int );
    
    private:
       short a;
       short b;
    };
    #endif
    example.cpp

    Code:
    #include "example.h"
    
    MyClass::MyClass(){}
    MyClass::~MyClass() {}
    
    void MyClass::swap (   // code  )
    {
       // swap function code here
    }
    Double Helix STL

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Is there a reason I should be using classes?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    there are several reasons to use classes... but they have nothing to do with the original question.

    header files should contain only function prototypes
    function bodies should be put into c-files.

    in this case - you bring all h-files with you, and only the c-file(s), containing functions that are to be linked with the main
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Would I put a class definition in a .h file and all of its member function in .cpp file?

    If one of my member functions calls a c++ function, is there a way that the c++ function can be included in the class?

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by thetinman
    Would I put a class definition in a .h file and all of its member function in .cpp file?
    Yes you usually put the description for the class in the header file and the actual source for the member functions in a separate source file; unless it's a templated class in which case all the code would likely need to be in the header.


    Quote Originally Posted by thetinman
    If one of my member functions calls a c++ function, is there a way that the c++ function can be included in the class?
    Yes, there should be no problems simply calling the desired function from within the class as appropriate. As long as the appropriate header is included by that source file, then the compiler should not complain during the compilation step. It is the linkers job to actually bring in the code for the called function into the final executable and unless the function exists in an odd library file somewhere then you shouldn't have to do anything special to tell the linker where the function's code exists since most of the basic library files are likely included in the default linkage settings.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM