"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:yT****************@newsread2.news.pas.earthli nk.net...
"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:Yu********************@comcast.com... "E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:3F************@jpl.nasa.gov... 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
combine the code?
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.
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