"SK" <onesolution@sb cglobal.net> wrote in message
news:IW6vb.4707 $Uq3.2307031200 @newssvr30.news .prodigy.com...[color=blue]
> I have a c++ program that spans multiple files. How do I call member
> functions in one file from the other?[/color]
1. Please don't mult-post, but cross-post instead if you
want to post the same message to more than one group.
2. See my reply to the same message you posted at
acllc-c++.
3. Had you observed netiquette and done #1 above, I would
not be wasting my time writing this.
SK wrote:
[color=blue]
> I have a C++ program that spans multiple files.
> How do I call member functions in one file from the other?[/color]
The class definition must appear in *every* translation unit.
Usually, the class definition is placed in a header file
which is included near the top of every source file
(translation unit).
Your link editor will load all of the object files
created by your compiler and resolve all of the undefined links
to functions defined in other files.
Mike Whaler gives good general advice
which you would do well to follow.
"E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message
news:3FBD24DB.3 0906@jpl.nasa.g ov...[color=blue]
> SK wrote:
>[color=green]
> > I have a C++ program that spans multiple files.
> > How do I call member functions in one file from the other?[/color]
>
> The class definition must appear in *every* translation unit.[/color]
Really? I thought it only needed the declaration. Won't the linker combine
the code?
--
Gary
"Gary Labowitz" <glabowitz@comc ast.net> wrote in message
news:YuWdnQiAx-2aoiCiRVn-gQ@comcast.com. ..[color=blue]
> "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message
> news:3FBD24DB.3 0906@jpl.nasa.g ov...[color=green]
> > SK wrote:
> >[color=darkred]
> > > I have a C++ program that spans multiple files.
> > > How do I call member functions in one file from the other?[/color]
> >
> > The class definition must appear in *every* translation unit.[/color]
>
> Really? I thought it only needed the declaration. Won't the linker combine
> the code?[/color]
This is not really a linker issue. What (I think) Robert
is trying to say is that:
class X;
X x;
of course will not work, because there's no information
about how to create a type 'X' object.
But
class X;
X *x;
is OK, since no object of type 'X' is being created.
"Gary Labowitz" <glabowitz@comc ast.net> wrote in message news:YuWdnQiAx-2aoiCiRVn-gQ@comcast.com. ..
[color=blue]
> Really? I thought it only needed the declaration. Won't the linker combine
> the code?[/color]
The definition is the thing that has all the members declared. Just about
any use of the class (other than declaring pointers to it), needs the definition.
Your original question wasn't very clear but I got the impression you're
wanting to access functions in an implementation file for user defined classes
that form project files. i.e.
In which case you declare an object of type 'myClass' within the Client file
and use dot notation. Supose myClass contains a function 'int func(int, int)'.
within client file:
#include "myClass.h"
int main()
{
myClass x;
int a = 2;
int b = 10;
x.func(a,b);
..
..
..
}
Unless I totally misunderstood the original question.
"Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
news:yTbvb.7514 $sb4.870@newsre ad2.news.pas.ea rthlink.net...[color=blue]
>
> "Gary Labowitz" <glabowitz@comc ast.net> wrote in message
> news:YuWdnQiAx-2aoiCiRVn-gQ@comcast.com. ..[color=green]
> > "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message
> > news:3FBD24DB.3 0906@jpl.nasa.g ov...[color=darkred]
> > > SK wrote:
> > >
> > > > I have a C++ program that spans multiple files.
> > > > How do I call member functions in one file from the other?
> > >
> > > The class definition must appear in *every* translation unit.[/color]
> >
> > Really? I thought it only needed the declaration. Won't the linker[/color][/color]
combine[color=blue][color=green]
> > the code?[/color]
>
> This is not really a linker issue. What (I think) Robert
> is trying to say is that:
>
> class X;
> X x;
>
> of course will not work, because there's no information
> about how to create a type 'X' object.
>
> But
>
> class X;
> X *x;
>
> is OK, since no object of type 'X' is being created.[/color]
I'm still not getting it. I supposed the OP wanted to call a member function
of the class in a file which didn't contain the class definition. If he
included (say) a header that contained the class declaration, the prototype
of the function would be in it. That is:
File X.h
#ifndef X_HEADER
#define X_HEADER
class X
{
public:
void display( );
};
#endif
---end-----
File RunX.cpp
#include "X.h"
int main( )
{
X myX;
myX.display( );
}
---end----
File UtilX.cpp
#include <iostream>
#include "X.h"
void X::display()
{
std::cout << "Here I am" << std::endl;
}
---end-----
Works for me.
I've always assumed this will work because the linker will find the code for
the X::display function in an object file (if it exists and the libraries
are searched properly) and link in the needed code and resolve the call. No?
Or am I just referring to the class information above wrongly?
--
Gary
"Gary Labowitz" <glabowitz@comc ast.net> wrote in message
news:YridnXlZyY NkwyCiRVn-hg@comcast.com. ..[color=blue]
> "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
> news:yTbvb.7514 $sb4.870@newsre ad2.news.pas.ea rthlink.net...[color=green]
> >
> > "Gary Labowitz" <glabowitz@comc ast.net> wrote in message
> > news:YuWdnQiAx-2aoiCiRVn-gQ@comcast.com. ..[color=darkred]
> > > "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message
> > > news:3FBD24DB.3 0906@jpl.nasa.g ov...
> > > > SK wrote:
> > > >
> > > > > I have a C++ program that spans multiple files.
> > > > > How do I call member functions in one file from the other?
> > > >
> > > > The class definition must appear in *every* translation unit.
> > >
> > > Really? I thought it only needed the declaration. Won't the linker[/color][/color]
> combine[color=green][color=darkred]
> > > the code?[/color]
> >
> > This is not really a linker issue. What (I think) Robert
> > is trying to say is that:
> >
> > class X;
> > X x;
> >
> > of course will not work, because there's no information
> > about how to create a type 'X' object.
> >
> > But
> >
> > class X;
> > X *x;
> >
> > is OK, since no object of type 'X' is being created.[/color]
>
> I'm still not getting it. I supposed the OP wanted to call a member[/color]
function[color=blue]
> of the class in a file which didn't contain the class definition. If he
> included (say) a header that contained the class declaration, the[/color]
prototype[color=blue]
> of the function would be in it. That is:
> File X.h
>
> #ifndef X_HEADER
> #define X_HEADER
> class X
> {
> public:
> void display( );
> };
> #endif
> ---end-----
> File RunX.cpp
>
> #include "X.h"
> int main( )
> {
> X myX;
> myX.display( );
> }
> ---end----
> File UtilX.cpp
>
> #include <iostream>
> #include "X.h"
> void X::display()
> {
> std::cout << "Here I am" << std::endl;
> }
> ---end-----
> Works for me.
> I've always assumed this will work because the linker will find the code[/color]
for[color=blue]
> the X::display function in an object file (if it exists and the libraries
> are searched properly) and link in the needed code and resolve the call.[/color]
No?[color=blue]
> Or am I just referring to the class information above wrongly?[/color]
I think you've got it right. I must have misunderstood. I thought
you were talking about invoking a member function whose prototype
was not in scope. Sorry for the confusion.
Comment