S
SK
I have a c++ program that spans multiple files. How do I call member
functions in one file from the other?
Thanks,
Santosh
functions in one file from the other?
Thanks,
Santosh
SK said:I have a c++ program that spans multiple files. How do I call member
functions in one file from the other?
SK said:I have a C++ program that spans multiple files.
How do I call member functions in one file from the other?
E. Robert Tisdale said:The class definition must appear in *every* translation unit.
Gary Labowitz said:Really? I thought it only needed the declaration. Won't the linker combine
the code?
Gary Labowitz said:Really? I thought it only needed the declaration. Won't the linker combine
the code?
Mike Wahler said: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 said: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?
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.