468,484 Members | 1,859 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

Calling a member function from a separate class

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.cpp" (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::CreateGrid()
{
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 "DrawSpreadsheet.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 5202
Banfa
9,055 Expert Mod 8TB
Your variable

wxGrid* grid;

is declared in the scope of the function

wxGrid* MyFrame::CreateGrid()

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
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(wxWindow* parent,
wxWindowID id,
const wxString& title,
const wxPoint& pos = wxDefaultPosition,
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 'DrawSpreadsheet.cpp', as below (watch for "-->")

(excerpt from "DrawSpreadsheet.cpp")

#include <stdio.h>
#include "theApp.h"
--> #include "theFrame.h"
#include "DrawSpreadsheet.h"
#include "wxIrrlichtWindow.h"
#include "Protocol.h"
#include "ClientSocket.h"

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

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

DrawSpreadsheet::DrawSpreadsheet()
{
// 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,055 Expert Mod 8TB
In excerpt from "DrawSpreadsheet.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

Post your reply

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

Similar topics

reply views Thread by slide_o_mix | last post: by
30 posts views Thread by Joost Ronkes Agerbeek | last post: by
10 posts views Thread by jim.brown | last post: by
3 posts views Thread by Ken | last post: by
4 posts views Thread by Jerry Krinock | last post: by
13 posts views Thread by RainBow | last post: by
3 posts views Thread by Tomás | last post: by
1 post views Thread by newbie | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.