Having problems using multiple files

This is a discussion on Having problems using multiple files within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm workin on using a header file with 2 .cpp files to find the volume of a cylinder. Heres ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    6

    Having problems using multiple files

    Hi, I'm workin on using a header file with 2 .cpp files to find the volume of a cylinder. Heres what I have for the header file.
    Code:
    class cylinder
    
    {
    
    	private:
    		double r,l,v;
    
    	public:
    		double cylvol();
    		  	 cylinder();
    		void get_data();
    };
    the name for the header file is cylinder.cpp
    ::this is cylinder.cpp::
    Code:
    #include "cylinder.h"
    #include <iostream>
    
    using namespace std;
    
    void cylinder::get_data()
    
    {
    	cout << "Please enter the radius and length of the Cylinder" << endl;
    	cin >> r >> l;
    
    	return;
    };
    
    double cylinder::cylvol()
    
    {
    	const double PI = 3.14157;
    	v=PI*r*r*l;
    	
    	return v;
    };
    
    cylinder::cylinder()
    
    {
    	r = 0.0;
    	l = 0.0;
    	v = 0.0;
    };
    and last but not least the main program called foo.cpp
    Code:
    #include "cylinder.cpp"
    
    int main()
    
    {
    	cylinder junk;
    
    	junk.get_data();
    	
    	cout << "The volume of the cylinder is: " << junk.cylvol() << endl;
    
    	return 0;
    };
    I get 3 link errors, please tell me what I'm doing wrong

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    In your main file replace #include "cylinder.cpp" with #include "cylinder.h" and add cylinder.cpp to your project.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    You include the header in your main program not the cpp file
    Woop?

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    6
    Hey thanks guys, figured it out already though. Had to change that one source file to a header file. Hey by the way can you have more than one source file like how I have it in this program?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    What 3 linker errors? What compiler are you using?

    Header files should generally not contain code (unless this is some templated code). You also should not be including one source file from another source file. Both cylinder.cpp and foo.cpp should include cylinder.h but that is it. The compiler then compiles the two source files into object files, cylinder.obj and foo.obj (for example). Then the linker combines the object files along with code from its libraries to produce the executable.

    How you do the compiling and linking of a multifile program depends on the compiler you are using. If using an IDE of some kind this usually involves setting up a project and adding all your source files to the project and then hitting the "build" button. If using a command-line compiler/linker, then you need to specify all the source files you are using when you do the compilation/linking steps, the exact syntax of which will depend on your compiler/linker.
    "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

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Mess around with the #ifndef and #endif preprocessor directives. This will allow better header inclusions because it will prevent recursive inclusion problems. There were some topics about this on this BBS, do a search for them

    Most compilers I've seen allow multiple .cpp files, I don't know of anything current that doesn't. You might want to also check your compiler documentation for the specifics.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    As Tronic said, you should look at the preprocessor statements. You will get linker errors if you two cpp files use the same include file, so to make sure that the include file only gets declared once, change your .h file to this:

    Code:
    #ifndef __CYLINDER_H //if something is not already defined as __CYLINDER_H
    #define __CYLINDER_H //then define it and allow the following code
    class cylinder
    
    {
    
    	private:
    		double r,l,v;
    
    	public:
    		double cylvol();
    		  	 cylinder();
    		void get_data();
    };
    #endif
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM
  2. Opening Multiple Files in sequence
    By wujiajun in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2006, 07:47 PM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  4. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 09:57 PM
  5. Multiple .c files
    By GaPe in forum Windows Programming
    Replies: 3
    Last Post: 03-26-2002, 07:27 AM

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