question on member functions

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
 
M

Mike Wahler

SK said:
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
 
E

E. Robert Tisdale

SK said:
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.
 
G

Gary Labowitz

E. Robert Tisdale said:
The class definition must appear in *every* translation unit.

Really? I thought it only needed the declaration. Won't the linker combine
the code?
 
M

Mike Wahler

Gary Labowitz said:
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
 
R

Ron Natalie

Gary Labowitz said:
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.
 
J

jbruno4000

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

jbruno4000

Sorry, to state it more correctly:

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

c = x.func(a,b);
..
 
G

Gary Labowitz

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.

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?
 
M

Mike Wahler

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?

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
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top