473,659 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quick question:

Hello:

Can we pass a pointer to an object from one cpp file to another by using a
function call. Assuming the object was created and declared along with its
accessor methods in the first cpp file, but you need to manipulate the same
object in the second cpp file.

heres a quick example of the function call which calls a function
in the second cpp file:

=============== ===cpp #1
#include "cpp2.h"
class components
{
public:
void SYS_innitz_FLAG _AllTrueInCase( ) {}
virtual void SYS_set_SFCC(bo ol bCondTrue,int AmtOfComm) {}
protected:
};

static components cp;

SFC1(&cp); //Calling a function which exists in cpp #2
=============== ========

=============== ===cpp2.h - header for cpp2
void SFC1(components *cp); //Function declaration
=============== =======
=============== ===cpp #2
void SFC1(components *cp) //Function implementation
{}
=============== =======

When I do this I get the obvious errors of:

c:\_DTS_PROGRAM MING\C_PROGRAMM ING\vc++\MY_APP S_LAB\XPPLC\SFC _1.h(19): error
C2065: 'components' : undeclared identifier

c:\_DTS_PROGRAM MING\C_PROGRAMM ING\vc++\MY_APP S_LAB\XPPLC\SFC _1.h(19): error
C2065: 'cp' : undeclared identifier
It seems that all cpp files *must* have their own class definitions, and
associated implementations ... right? I do beleive that I did post this
problem before at a much larger scale, however, there could be a wrinkle of
difference in this one. Basically, just double checking!

All welcome and appreciated for the feedback....

thanks!

--
Best regards
Robert
May 28 '06 #1
1 1054
Robby wrote:
Hello:

Can we pass a pointer to an object from one cpp file to another by
using a function call. Assuming the object was created and declared
along with its accessor methods in the first cpp file, but you need
to manipulate the same object in the second cpp file.
Yes, of course. There's nothing magical about what is in a single source
file. If your code is properly structured (i.e. you're not violating the
one definition rule anywhere and you're #including the right headers
everywhere) then there's no difference between putting your entire program
in a single source file or putting each and every function in a separate
source file.

heres a quick example of the function call which calls a function
in the second cpp file:

=============== ===cpp #1
#include "cpp2.h"
class components
{
public:
void SYS_innitz_FLAG _AllTrueInCase( ) {}
virtual void SYS_set_SFCC(bo ol bCondTrue,int AmtOfComm) {}
protected:
};

static components cp;

SFC1(&cp); //Calling a function which exists in cpp #2
=============== ========

=============== ===cpp2.h - header for cpp2
void SFC1(components *cp); //Function declaration
=============== =======
=============== ===cpp #2
void SFC1(components *cp) //Function implementation
{}
=============== =======

When I do this I get the obvious errors of:

c:\_DTS_PROGRAM MING\C_PROGRAMM ING\vc++\MY_APP S_LAB\XPPLC\SFC _1.h(19):
error C2065: 'components' : undeclared identifier
c:\_DTS_PROGRAM MING\C_PROGRAMM ING\vc++\MY_APP S_LAB\XPPLC\SFC _1.h(19):
error C2065: 'cp' : undeclared identifier
Remember that the compiler sees only what you "feed" to it. Does cpp #2
#include the definition of the class "components " anywhere? If not, then
the compiler can't see it.
It seems that all cpp files *must* have their own class definitions,
and associated implementations ... right?


No. A .cpp file is simply a unit of code that the compiler processes - but
each .cpp file is processed in isolation. Nothing that's declared in one
file is visible in another file. That's why we have #include and the
convention of using "header files" - common definitions and declaration are
placed in header files that are then included into all of the compilation
units that need them.

The most common way of structuing C++ programs is to put class definitions
into header files and the implementation of functions (be they member
functions or free functions) into compilation units (i.e. ".cpp files").
That way you can #include the defintion of a class into as many compilation
units as you require.

-cd
May 28 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
8427
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8525
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.