473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
6 2451

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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phillip Montgomery | last post by:
Hello all; I'm trying to debug an issue with a java script called, SelectSockets. It appears to be a fairly common one found on the web. I downloaded the SGI Java v1.4.1 installation from SGI's...
7
by: news | last post by:
I am pretty new to Java and I just install the JDK1.4.2 03 I am getting an error when I run the class file TestChart.class with the java.exe: Exception in thread "main"...
2
by: dave | last post by:
(Forgive all caps... they are there to differentiate btw question and code) THIS IS THE ERROR I KEEP GETTING. IT COMPILES BUT WHEN IT RUNS, THE FOLLOWING POPS UP: Exception in thread "main"...
4
by: Don | last post by:
Is there some way to redirect the "main" frame to another URL from within the "header" frame? Thanks, Don ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----...
1
by: Siv Hansen | last post by:
I have a function query(String $query) in my DbConnector-class- goes like this: function query($query) { $mysqlQuery = mysql_query($query, $this->link); if(!$mysqlQuery){...
7
by: David Elliott | last post by:
I have created an application that will dynamically load other DLLs (plugins). The new plugin is a winform with an embedded IE Browser. I am wanting to have the form to run in its own thread....
25
by: mdh | last post by:
Hi all, Going quite methodically through K& R ( as some of you can attest to!), I have never seen a big diffference in declaring a function within "main" or "ahead" of it. Now, (p119, K&R II), the...
12
nomad
by: nomad | last post by:
Hi everyone; My Class has ended and I was not able to solve this problem in time, and I would still like to solve it. I got these error code. Exception in thread "main"...
2
by: lilyumestar | last post by:
This project is due by Tuesday and I haven't even gotten half of it done. Can anyone please help me with this Exception error? I've been trying to figure it out for several hours Error Message ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.