By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

question on member functions

P: n/a
SK
I have a c++ program that spans multiple files. How do I call member
functions in one file from the other?

Thanks,
Santosh
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"SK" <on*********@sbcglobal.net> wrote in message
news:IW***********************@newssvr30.news.prod igy.com...
I have a c++ program that spans multiple files. How do I call member
functions in one file from the other?


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.

4. Where is your textbook?

-Mike
Jul 22 '05 #2

P: n/a
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.
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.

Jul 22 '05 #3

P: n/a
"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?
--
Gary
Jul 22 '05 #4

P: n/a

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

-Mike
Jul 22 '05 #5

P: n/a

"Gary Labowitz" <gl*******@comcast.net> wrote in message news:Yu********************@comcast.com...
Really? I thought it only needed the declaration. Won't the linker combine
the code?


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.

The definition of the members can be elsewhere.
Jul 22 '05 #6

P: n/a
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.

Client file: myApplication.cpp
Definition file: myClass.h
Implementation file: myClass.cpp

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.


Jul 22 '05 #7

P: n/a
Sorry, to state it more correctly:

int main()
{
myClass x;
int a = 2;
int b = 10;
int c;

c = x.func(a,b);
..
Jul 22 '05 #8

P: n/a
"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
Jul 22 '05 #9

P: n/a

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:Yr********************@comcast.com...
"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?


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.

-Mike
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.