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

J

Jofio

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
 
J

Jaspreet

Jofio said:
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.
 
M

Michiel.Salters

Jofio said:
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
 
J

Jofio

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
 
I

Ian Collins

Jofio said:
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>
 
J

Jofio

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.
 
R

Roberto Waltman

Jofio said:
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.'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top