473,666 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a member function from a separate class

2 New Member
Hello to all!

1a. I'm a newbie having trouble accessing a pointer from one .cpp file which is defined within a member function found in a separate .cpp file. I'm looking for a line or two of sample correct syntax, and/or a philosophical hint or two about structuring such requests.

Consider the following code snippet from "theFrame.c pp" (written for VC++ 2005 Express), attached. Here, a pointer 'grid' is defined, which is then used a few lines later to call wxWidgets 'spreadsheet' functions. This code works in context, creating a spreadsheet 100 rows x 13 columns, and then modifying an axis label.

wxGrid* MyFrame::Create Grid()
{
wxGrid* grid = new wxGrid(this, wxID_ANY,
wxPoint(0,0),
wxSize(150,250) ,
wxNO_BORDER | wxWANTS_CHARS);

// Create a Grid

grid->CreateGrid( 0, 0 );
grid->AppendRows(100 );
grid->AppendCols(13) ;

// Name the columns
grid->SetLabelValue( wxHORIZONTAL,_T ("ItemID"),0 );

1b. I'd like to access 'grid' from another class, in another file. For instance, in "DrawSpreadshee t.cpp" I'd like to call "grid->CreateGrid( 0, 0 );" as above. I suspect "extern wxGrid *grid" is not a valid way to bring the 'grid' pointer into this new file -- it generates an unresolved external message from the compiler. How can I access the 'grid' pointer from classes in other files?

Thanks in advance for any insight you might be able to shed...

p.s.--The attached code references wxGrid within the wxWidgets library.
Sep 11 '06 #1
3 5445
Banfa
9,065 Recognized Expert Moderator Expert
Your variable

wxGrid* grid;

is declared in the scope of the function

wxGrid* MyFrame::Create Grid()

It will never be viewable by any code external to this function except functions that are called by this function and passed grid as a parameter.

To access grid from any external code (in this file or another file) it needs to be declared at a different scope. You have 2 choices

1. Declare grid at file scope. Declare grid at the top of the file, then you can use a simple extern statement in your other c files (or preferably a header) to access it.

2. Declare grid at class scope. Declare grid as a member of MyFrame (possibly public). Then external files can access it through the current instance of MyFrame either directly or using a member function to return it.


Since global data is frowned upon in C++ (and C as well to some extent) I would suggest option 2, make it a member of MyFrame.
Sep 11 '06 #2
pelleas
2 New Member
Your variable
2. Declare grid at class scope. Declare grid as a member of MyFrame (possibly public). Then external files can access it through the current instance of MyFrame either directly or using a member function to return it.
Thanks, Banfa, for answering my (silly) question with great kindness...that 's much appreciated.

I realize that global variables are discouraged in C++, so I'd like to declare 'grid' as a public member of MyFrame, as you suggest. Here's what I attempted: (watch for "-->")

---

(excerpt from "theFrame.h ")

class MyFrame : public wxFrame
{

public:
MyFrame(wxWindo w* parent,
wxWindowID id,
const wxString& title,
const wxPoint& pos = wxDefaultPositi on,
const wxSize& size = wxDefaultSize,
long style = wxDEFAULT_FRAME _STYLE | wxSUNKEN_BORDER );

~MyFrame(); //prototype for destructor

--> wxGrid* grid;
...

I'm attempting to refer to data in 'DrawSpreadshee t.cpp', as below (watch for "-->")

(excerpt from "DrawSpreadshee t.cpp")

#include <stdio.h>
#include "theApp.h"
--> #include "theFrame.h "
#include "DrawSpreadshee t.h"
#include "wxIrrlichtWind ow.h"
#include "Protocol.h "
#include "ClientSocket.h "

#include "wx/grid.h"
#include "wx/generic/gridctrl.h"

extern MyApp& wxGetApp();
--> extern wxGrid* grid;

DrawSpreadsheet ::DrawSpreadshe et()
{
// Name a column as a test
grid->SetLabelValue( wxHORIZONTAL,_T ("TEST"),0);
...
I clearly have my syntax wrong and am receiving unresolved external symbol error from the compiler from DrawSpreadsheet .cpp. Would it be possible for you point out my errors?

Thanks again for your help!
Sep 11 '06 #3
Banfa
9,065 Recognized Expert Moderator Expert
In excerpt from "DrawSpreadshee t.cpp" look for <--

--> #include "theFrame.h " <-- this is good and what is required

--> extern wxGrid* grid; <-- this is completely unrequired


DrawSpreadsheet :rawSpreadsheet ()
{
// Name a column as a test
grid->SetLabelValue( wxHORIZONTAL,_T ("TEST"),0); <-- wrong syntax

<-- you need an instance or pointer to a MyFrame object

MyFrame *pFrame = GetMyFrame();

pFrame->grid->SetLabelValue( wxHORIZONTAL,_T ("TEST"),0);

...
}


For the purposes of this example I have created the imaginary function

MyFrame *GetMyFrame()

You will have to find a way to implement/replace it.
Sep 11 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2958
by: slide_o_mix | last post by:
I have a class that has an array of function pointers as a member variable. I pass in function pointers in the constructor (these function pointers are to C functions). Later when I try and call the function, it says there is an error and my program crashes (assuming a memory problem). Here is a small example of what I am trying to do.
30
3470
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
10
3204
by: jim.brown | last post by:
Please refer me to the right place if this is the wrong place to post this question. I'm looking for an example of calling the Eigenvalue routines of JAMA from a C++ program. The documentation says that the is the public method: Eigenvalue (const TNT::Array2D< Real > &A) I write this program
3
2386
by: Ken | last post by:
hello, I would to know if it is possible to call an object in a function within a class. Meaning , In a class, A function X calling onto a function Y, and function Y we want one of the two calculation ( eg seconds , out of seconds and minutes) thanks , Ken
4
7279
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the constructor again. In my larger program, I find that the member variables don't get re-initialized when "reconstructed". I don't have that problem in this demo, but the second time the constructor is called, its "this" points to a different location. ...
13
4101
by: RainBow | last post by:
Hi everyone, (Very Sorry, if this is the wrong group in which I am posting this query). Code snippet: //C library typedef int (*PFunc)(int* aArg); void call_c_foo(PFunc aPtrtoFunc) {
6
4401
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the same invocation from a DistantlyRelated class. What actually happened was that the compiler produced: error C2247: 'A::function' not accessible because 'CloselyRelated' uses 'private' to inherit from 'A' I'm guessing that the above compiler...
3
1568
by: Tomás | last post by:
class Monkey { int cow() { static int k = 4; ++k; return k; }
1
8528
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it would be great if the following practice is valid. But by OO concept, it seems not to very good because init() belongs to an object, which hasn't be constructed. Can you give any comment on my question?
0
8449
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
8360
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
8876
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8784
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
8556
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
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1777
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.