GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Dec-2008, 21:58
scuzzo scuzzo is offline
Awaiting Email Confirmation
 
Join Date: Jul 2007
Posts: 19
scuzzo is an unknown quantity at this point

Class definitions


I have a complete file that compiles easily. I need to separate out the class and function prototypes into one file, the function definitions into another file and leave the main body in the original file. I can get it to work with just the header.h and main.cpp, but when I put the class function definitions into another file I can't figure out how to get that third file included.
  #2  
Old 12-Dec-2008, 23:23
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: class definitions


#include "the-third-file"
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #3  
Old 13-Dec-2008, 00:05
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: class definitions


Quote:
Originally Posted by WaltP
#include "the-third-file"
I think that is a terrible thing to suggest, since, in my opinion, there is rarely a good reason to include a source file in another, and there are lots of situations where you do not want to (and, in fact, can not) include a source file in other files. I mean if you only had two source files and one header file, I guess there might not be any harm in #including one source file in the other, but it's just not the right way to learn this stuff.

The problem of linking together separate source files is a matter of how the project is being compiled.

If it is being compiled from a command line, and the compiler is GNU g++, for example, then a single file might be compiled by something like
Code:
g++ -Wall -W -pedantic testit.cpp -o testit

A command line to compile and link two source files together might look like
Code:
g++ -Wall -W -pedantic testit.cpp classdefs.cpp -o testit

In other words, you can simply put all of the source file names (not the header(s)) on the command line.

It works the same way with various other compilers, too.

If you are using a Microsoft command line compiler, cl.exe, then a single file would be compiled with something like:

Code:
cl testit.cpp /EHsc

And for two source files to be compiled and linked together you could have something like
Code:
cl testit.cpp classdefs.cpp /EHsc

If you are compiling from an Integrated Development Environment like Visual Studio or Borland Builder, then you should learn how to add multiple files to the project so that they will all get linked together.

Regards,

Dave
  #4  
Old 13-Dec-2008, 01:27
zatora zatora is offline
Member
 
Join Date: May 2008
Posts: 110
zatora will become famous soon enough

Re: class definitions


Quote:
Originally Posted by scuzzo
I have a complete file that compiles easily. I need to separate out the class and function prototypes into one file, the function definitions into another file and leave the main body in the original file. I can get it to work with just the header.h and main.cpp, but when I put the class function definitions into another file I can't figure out how to get that third file included.
If you are using visual studio this what you should do open an empty project
and name it .
then go to header file insert your code it should lokks like this
CPP / C++ / C Code:
class sample
{
private:
// here ur private  members;
public:
// usuaully ur default and parameters constructors
sample();
sample(dataType);
void print() const;
// these are just exples not ur actual code
};
now you go to the folder resources and create your cpp file, which will contain the actual code it should looks like that
CPP / C++ / C Code:
inculde<iostream>
include"sample.h"
using name space std;

sample::sample()
{// code the default constructor;}
sample::sample(dataType)
{//code the parameter constructor;}
void sample::print()const
{//code what you want to print for example;}
now one you are done you go to the third folder and create your main.cpp
CPP / C++ / C Code:
#include<iostream>
#include"sample.h"
using namespace std;
int main()
{
sample obj(dataType);
sample.print();
return 0;
}
press Ctrl+F5 to run it.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning a Struct to a separate class usmsci C++ Forum 9 07-Feb-2007 09:34
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 20:23
Box Class, need help again :( TransformedBG C++ Forum 7 13-Nov-2006 16:11
C++ class -- Please help vnca_1 C++ Forum 3 14-Jun-2006 13:31
a tester class and then some. postage Java Forum 1 06-May-2006 16:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:03.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.