By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,660 Members | 1,961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,660 IT Pros & Developers. It's quick & easy.

How Can I link a .cp file in the "main" header?

P: n/a
I have a .h (header file) linked using #include<...>. I also have a .cp
file which i don't know how to include in the header of my "main" file.
I have the following c++ file.
The file names are: 1) dArray.h
2) dArray.cp
3) dArrayTest.cpp

dArray.h file declares and defines a class called DynamicArray.
dArray.cp defines the functions declared as member functions in
DynamicArray class in dArray.h. This dArray.h is includded in the
header of aArray.cp with #include<...> precompiler directive.

Question is: How do I include dArray.cp in the main program called
dArrayTest.cpp? Do I use the same pre-compiler directive? And do all
the 3 files need to be in the same folder for the calling file to "see"
the included file?

Any help on this is appreciated.

Jofio

Apr 10 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

Jofio wrote:
I have a .h (header file) linked using #include<...>. I also have a .cp
file which i don't know how to include in the header of my "main" file.
I have the following c++ file.
Put a
#include "dArray.cp"
at top of your dArrayTest.cpp file.
The file names are: 1) dArray.h
2) dArray.cp
3) dArrayTest.cpp

dArray.h file declares and defines a class called DynamicArray.
dArray.cp defines the functions declared as member functions in
DynamicArray class in dArray.h. This dArray.h is includded in the
header of aArray.cp with #include<...> precompiler directive.

Question is: How do I include dArray.cp in the main program called
dArrayTest.cpp? Do I use the same pre-compiler directive? And do all
the 3 files need to be in the same folder for the calling file to "see"
the included file?
Not necessary for all the files to be in the same folder. If I have
dArray.h in folder x,
I just have to do a #include "x/dArray.h" in dArray.cp.

Any help on this is appreciated.

Jofio


Apr 10 '06 #2

P: n/a
Jofio wrote:
I have a .h (header file) linked using #include<...>. I also have a .cp
file which i don't know how to include in the header of my "main" file.
I have the following c++ file.
The file names are: 1) dArray.h
2) dArray.cp
3) dArrayTest.cpp

dArray.h file declares and defines a class called DynamicArray.
dArray.cp defines the functions declared as member functions in
DynamicArray class in dArray.h. This dArray.h is includded in the
header of aArray.cp with #include<...> precompiler directive.

Question is: How do I include dArray.cp in the main program called
dArrayTest.cpp? Do I use the same pre-compiler directive? And do all
the 3 files need to be in the same folder for the calling file to "see"
the included file?


Almost all C++ implementations use two phases. In the first phase,
the compiler compiles every translation unit. A translation unit is
a source file plus all its #include's. Typically, that's one .cpp file,
some standard headers like <algorithm> and your own .h headers.
I.e. .h files are *included*.

In the second phase, the translated unites are *linked* together,
and the tool is of course called a linker. Basically this glues the
..cpp files from phase 1 together.

In your case, the dArray.cpp file should be linked to dArrayTest.cpp
by the linker, not by an include. Unlike #include, the mechanism for
that is left to the implementation. Usually there's some kind of
makefile or project file.

HTH,
Michiel Salters

Apr 10 '06 #3

P: n/a
I have just done what you advised. I recompiled and it gave me an error
message :

'Unable to open include file 'dArray.h'
'Unable to open include file'dArray.cp'

The result is that the compiller doesn't see the variables declared in
the 'main' program - dArrayTest.cpp - and used as an instance of the
class declared and defined in the dArray.h and dArray.cp file.

I don't seem to see any errors though ...

Here is my code in dArray.h ...
////////////////////////////////////////////////////////////////////////////////
#ifndef _MYDYNAM_
#define _MYDYNAM_

class DynamicArray{
public:
DynamicArray(int size=10, int inc=5); //default parameters

int Length(void) const; //length of the array
int Position(void *item) const; //returns the index of an
array as a position of the item whose value is pointed to by *item
void *Nth(int n) const; //will return a an address of the
nth value in the array

void Append(void *item); //adds a value pointed to by
*item

void *Remove(void *item);
void *Remove(int itempos);

private:
void Grow(int amount); //resizes the array dynamically

int fNum; //
int fSize; //the size of array
int fCSize;
int fInc; // size of increment
void **fItem; //
};

#endif
//////////////////////////////////////////////////////////////////////////////////

Here is the part of the code in the header section of dArray.cp ...

#include<stdlib.h>
#include<iostream.h>
#include<assert.h>
#include "dArray.h"
and here is the part of the code in the header section of my main
prog...

#include <iostream.h>
#include<string.h>
#include<stdlib.h>
#include "dArray.h" /* should this go as I have already included it
in the header of dArray.cp file */
#include "dArray.cp"

I don't think I made an error there as far as the linking/inclusions of
the .h and .cp files are concerned. however, I keep getting the error
messages I stated above.

Any help please?
Jofio

Apr 11 '06 #4

P: n/a
Jofio wrote:
I have just done what you advised. I recompiled and it gave me an error
message :

'Unable to open include file 'dArray.h'
'Unable to open include file'dArray.cp'

The result is that the compiller doesn't see the variables declared in
the 'main' program - dArrayTest.cpp - and used as an instance of the
class declared and defined in the dArray.h and dArray.cp file.

Have you told it where to look?

This usually takes the form of -I<path to your include files>

--
Ian Collins.
Apr 11 '06 #5

P: n/a
Thanks Ian.

All the files are in the same folder and so my assumption is that the
compiller will look into the current folder.

jofio

PS. The same problem is restated as a new topic so if you see it,
please ignore it.

Apr 11 '06 #6

P: n/a
"Jofio" wrote:
All the files are in the same folder and so my assumption is that the
compiller will look into the current folder.


A previous post suggests you are using the form '#include <header.h>'
instead of '#include "header.h"'
Many compilers will use separate include paths for each. The former
should be used for header files supplied with the compiler and/or
located in standard (for your system, that is,) directories.
The later for your own / project specific files.
If this doesn't help, you should specify to your compiler that the
current directory should be searched for include files.
For gcc, the syntax is '-I.'
Apr 11 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.