473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

question on member functions

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
9 1642
"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
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
"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

"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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class,...
3
by: paul.furber | last post by:
Hi all, I have some code which looks a bit like this: #define Offset(m, T) ((size_t)(&((T *)1)->m) - 1) class Point: private: int *x,*y;
7
by: Bob Morvay | last post by:
I am trying to determine how far I should go in encapsulating data. As I understand it, OO practices state to create private member variables and use these variables in your publicly accessible...
1
by: hunter hou | last post by:
Hello,Please look at the following code(from C++ in a nutshell) and my questions.Thanks,***Hunter... typedef void (*strproc)(const char*); void print(const char* str) { std::cout << "const...
2
by: sharpblade | last post by:
The title of these Item is "Make header files self-sufficient" There're two examples in this Item: Example 1: Dependent names. Templates are compiled at the point where they are defined, except...
4
by: sun1991 | last post by:
#include <iostream> using namespace std; class Base{ public: void ToString(){ ToStringCore(); } private: virtual void ToStringCore(){
4
by: JoeC | last post by:
I am trying to design some complex objects that have quite a bit of data. I understand most syntax but I am trying to learn how to make better design choices. The first question is to OK or good...
21
by: Noah Roberts | last post by:
How would you answer this question? "Describe the structure of a class." I was stumped, quite frankly.
8
by: amirr | last post by:
Guys, As you know static member functions do not have a this pointer. so instead, what shall i do? for example ... consider I want to call Code: ( text ) 1.
8
by: kevin | last post by:
Hello! So I was reading O'Reilly's C++ in a Nutshell when I came accross something interesting: The class definition for basic_ostream contains numerous overloaded operator<< functions: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.