473,769 Members | 7,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot access a class pointer from a different source file in the same project

8 New Member
I am trying to access a pointer to a class that I defined in a separate source file from another one. Here is an example of what I have:
Expand|Select|Wrap|Line Numbers
  1. //myclass.h file
  2. class testclass
  3. {
  4. public:
  5. testclass::testclass(void);
  6. int setintfunction(int testinteger);
  7. int getintfunction(void);
  8. testclass::~testclass(voide);
  9. private:
  10. int p_testinteger
  11. };
  12.  
  13. testclass::testclass(void)
  14. {
  15. p_testinteger = 0
  16. }
  17.  
  18. int testclass::setintfunction(int testinteger);
  19. {
  20. p_testinteger = testinteger;
  21. return 0;
  22. }
  23.  
  24. int testclass::getintfunction(void);
  25. {
  26. return p_testinteger;
  27. }
  28.  
  29. testclass::~testclass(void);
  30. {
  31. }
  32.  
  33. //mysourcefile1.cpp
  34. testclass* o_testclass = new testclass
  35. int main
  36. {
  37. ::o_testclass->setintfunction(13);
  38. }
  39.  
  40.  
  41. //mysourcefile2.cpp
  42. int testfunction
  43. {
  44. printf("%d",::o_testclass->getintfunction());
  45. }
  46.  
To put this in further context, mysourcefile1.c pp is my main application source file and mysourcefile2.c pp is actually a dialog source file. The mysourcefile2.c pp file is called when I make a dlg.domodal call. The problem is that I am trying to get information from a class I instantiated in the main source file but I keep getting an error C2039: 'blahblah' : is not a member of '`global namespace'' when I try to call a function from the class I instantiated in the main source file. The strange thing is that when I put :: in front of the pointer variable I get a global listing of it in visual c++ and also get a listing with I put a -> after it for its public functions...I am at a loss. Please help.

Thanks,

Liam
Jun 4 '07 #1
9 2240
gpraghuram
1,275 Recognized Expert Top Contributor
Hi,
There are couple of comilation errors in your code like jmissing semicolons and typo errors.
I have corrected the same .Try this
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. class testclass
  4. {
  5. public:
  6. testclass::testclass(void);
  7. int setintfunction(int testinteger);
  8. int getintfunction(void);
  9. ~testclass(void);
  10. private:
  11. int p_testinteger;
  12. };
  13.  
  14. testclass::testclass(void)
  15. {
  16. p_testinteger = 0;
  17. }
  18.  
  19. int testclass::setintfunction(int testinteger)
  20. {
  21. p_testinteger = testinteger;
  22. return 0;
  23. }
  24.  
  25. int testclass::getintfunction(void)
  26. {
  27. return p_testinteger;
  28. }
  29.  
  30. testclass::~testclass(void)
  31. {
  32. }
  33.  
  34. //mysourcefile1.cpp
  35. testclass* o_testclass = new testclass;
  36. int main()
  37. {
  38. ::o_testclass->setintfunction(13);
  39. }
  40.  
  41.  
  42. //mysourcefile2.cpp
  43. int testfunction()
  44. {
  45. printf("%d",::o_testclass->getintfunction());
  46. }
  47.  
Raghuram
Jun 4 '07 #2
phenrol
8 New Member
Thanks for the reply, but that did not work for me. I have a main source file that calls a dialog and I cannot get the dialog source file to use a class object that I created in the main source file. Here is the code that I have for a console MFC application:
Expand|Select|Wrap|Line Numbers
  1. //testclass.h file
  2. class testclass
  3. {
  4. public:
  5.     testclass::testclass(void);
  6.     int setintfunction(int testinteger);
  7.     int getintfunction(void);
  8.     ~testclass(void);
  9. private:
  10. int p_testinteger;
  11. };
  12.  
  13. testclass::testclass(void)
  14. {
  15.     p_testinteger = 0;
  16. }
  17.  
  18. int testclass::setintfunction(int testinteger)
  19. {
  20.     p_testinteger = testinteger;
  21. return 0;
  22. }
  23.  
  24. int testclass::getintfunction(void)
  25. {
  26.     return p_testinteger;
  27. }
  28.  
  29. testclass::~testclass(void)
  30. {
  31. }
  32.  
  33. //Dialog1.h file
  34. #if !defined(AFX_DIALOG1_H__96E75EE7_F10A_44B0_8B55_D525D4FD4519__INCLUDED_)
  35. #define AFX_DIALOG1_H__96E75EE7_F10A_44B0_8B55_D525D4FD4519__INCLUDED_
  36.  
  37. #if _MSC_VER > 1000
  38. #pragma once
  39. #endif // _MSC_VER > 1000
  40. // Dialog1.h : header file
  41. //
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CDialog1 dialog
  45.  
  46. class CDialog1 : public CDialog
  47. {
  48. // Construction
  49. public:
  50.     CDialog1(CWnd* pParent = NULL);   // standard constructor
  51.  
  52. // Dialog Data
  53.     //{{AFX_DATA(CDialog1)
  54.     enum { IDD = IDD_DIALOG1 };
  55.     CEdit    m_edit;
  56.     //}}AFX_DATA
  57.  
  58.  
  59. // Overrides
  60.     // ClassWizard generated virtual function overrides
  61.     //{{AFX_VIRTUAL(CDialog1)
  62.     protected:
  63.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  64.     //}}AFX_VIRTUAL
  65.  
  66. // Implementation
  67. protected:
  68.  
  69.     // Generated message map functions
  70.     //{{AFX_MSG(CDialog1)
  71.     afx_msg void OnButton1();
  72.     //}}AFX_MSG
  73.     DECLARE_MESSAGE_MAP()
  74. };
  75.  
  76. //{{AFX_INSERT_LOCATION}}
  77. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  78.  
  79. #endif // !defined(AFX_DIALOG1_H__96E75EE7_F10A_44B0_8B55_D525D4FD4519__INCLUDED_)
  80.  
  81.  
  82.  
  83. //lrsGlobalTest.cpp file
  84. #include "stdafx.h"
  85. #include "lrsGlobalTest.h"
  86. #include "testClass.h"
  87. #include "Dialog1.h"
  88.  
  89. #ifdef _DEBUG
  90. #define new DEBUG_NEW
  91. #undef THIS_FILE
  92. static char THIS_FILE[] = __FILE__;
  93. #endif
  94. testclass* o_testclass = new testclass;
  95. /////////////////////////////////////////////////////////////////////////////
  96. // The one and only application object
  97.  
  98. CWinApp theApp;
  99.  
  100. using namespace std;
  101.  
  102. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  103. {
  104.     int nRetCode = 0;
  105.  
  106.     // initialize MFC and print and error on failure
  107.     if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  108.     {
  109.         // TODO: change error code to suit your needs
  110.         cerr << _T("Fatal Error: MFC initialization failed") << endl;
  111.         nRetCode = 1;
  112.     }
  113.     else
  114.     {
  115.         // TODO: code your application's behavior here.
  116.         CString strHello;
  117.         strHello.LoadString(IDS_HELLO);
  118.         cout << (LPCTSTR)strHello << endl;
  119.         ::o_testclass->setintfunction(13);
  120.         printf("%d",::o_testclass->getintfunction());
  121.         CDialog1 dlg;
  122.         dlg.DoModal();
  123.  
  124.     }
  125.  
  126.     return nRetCode;
  127. }
  128.  
  129. //Dialog1.cpp file
  130. #include "stdafx.h"
  131. #include "lrsGlobalTest.h"
  132. #include "Dialog1.h"
  133.  
  134. #ifdef _DEBUG
  135. #define new DEBUG_NEW
  136. #undef THIS_FILE
  137. static char THIS_FILE[] = __FILE__;
  138. #endif
  139.  
  140. /////////////////////////////////////////////////////////////////////////////
  141. // CDialog1 dialog
  142.  
  143.  
  144. CDialog1::CDialog1(CWnd* pParent /*=NULL*/)
  145.     : CDialog(CDialog1::IDD, pParent)
  146. {
  147.     //{{AFX_DATA_INIT(CDialog1)
  148.         // NOTE: the ClassWizard will add member initialization here
  149.     //}}AFX_DATA_INIT
  150.     //AfxMessageBox("test");
  151. }
  152.  
  153.  
  154. void CDialog1::DoDataExchange(CDataExchange* pDX)
  155. {
  156.     CDialog::DoDataExchange(pDX);
  157.     //{{AFX_DATA_MAP(CDialog1)
  158.     DDX_Control(pDX, IDC_EDIT1, m_edit);
  159.     //}}AFX_DATA_MAP
  160. }
  161.  
  162.  
  163. BEGIN_MESSAGE_MAP(CDialog1, CDialog)
  164.     //{{AFX_MSG_MAP(CDialog1)
  165.     ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  166.     //}}AFX_MSG_MAP
  167. END_MESSAGE_MAP()
  168.  
  169. /////////////////////////////////////////////////////////////////////////////
  170. // CDialog1 message handlers
  171.  
  172. void CDialog1::OnButton1() 
  173. {
  174.     // TODO: Add your control notification handler code here
  175.     int testint;
  176.     char teststr[3];
  177.     sprintf(teststr, "%d", ::o_testclass->getintfunction());
  178.     m_edit.SetWindowText(teststr);
  179. }
  180.  
When I try to compile is says
C:\admin\vc++\l rsGlobalTest\Di alog1.cpp(51) : error C2039: 'o_testclass' : is not a member of '`global namespace''
C:\admin\vc++\l rsGlobalTest\Di alog1.cpp(51) : error C2065: 'o_testclass' : undeclared identifier
C:\admin\vc++\l rsGlobalTest\Di alog1.cpp(51) : error C2227: left of '->getintfunction ' must point to class/struct/union

In my code, line 51 is the sprintf(teststr , "%d", ::o_testclass->getintfunction ()); line.

Any ideas? A friend told me to try to use "extern" but I am unsure how to proceed with that idea.

Thanks
Jun 4 '07 #3
Savage
1,764 Recognized Expert Top Contributor

In my code, line 51 is the sprintf(teststr , "%d", ::o_testclass->getintfunction ()); line.

Any ideas? A friend told me to try to use "extern" but I am unsure how to proceed with that idea.

Thanks
Why don't u put class testclass inside ur namespace?

Savage
Jun 4 '07 #4
phenrol
8 New Member
Why don't u put class testclass inside ur namespace?

Savage
Because I want to use it in the main source file and also in my dialog source file. I need the information from the instantiated object in both source files. The strange thing is that it acutally shows up in the global namespace in the dialog source file, I just can't use it for some reason.
Jun 4 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
Here is your situation:
Expand|Select|Wrap|Line Numbers
  1. //mysourcefile1.cpp
  2. testclass* o_testclass = new testclass
  3.  
  4. //mysourcefile2.cpp
  5.  
As you can see o_testclass is not in mysourcefile2.c pp. To use it, you meuch declare it in mysourcefile2.c pp.

Expand|Select|Wrap|Line Numbers
  1. //mysourcefile1.cpp
  2. testclass* o_testclass = new testclass
  3.  
  4. //mysourcefile2.cpp
  5. testclass* o_testclass = new testclass
  6.  
But now you have TWO o_testclass variables and you want to use only the one in mysourcefile1.c pp. So, you use the extern storage class specifer and declare o_testclass to be a testclass* that is external to mysourcefile2.c pp

LIke this:
Expand|Select|Wrap|Line Numbers
  1. //mysourcefile1.cpp
  2. testclass* o_testclass = new testclass
  3.  
  4. //mysourcefile2.cpp
  5. extern testclass* o_testclass;
  6.  
The compiler when compiling mysourcefile2.c pp will use the o_testclass pointer but will mark it as an "unresolved external reference". The linker will pick up on this and tie all the uses of o_testclass in mysourcefile2.c pp to the pointer o_testclass in mysourcefile1.c pp.
Jun 4 '07 #6
phenrol
8 New Member
I tried this and get the following compiler errors when I use "extern testclass* o_testclass;" in the other source file.

Dialog1.obj : error LNK2005: "public: __thiscall testclass::test class(void)" (??0testclass@@ QAE@XZ) already defined in lrsGlobalTest.o bj
Dialog1.obj : error LNK2005: "public: int __thiscall testclass::seti ntfunction(int) " (?setintfunctio n@testclass@@QA EHH@Z) already defined in lrsGlobalTest.o bj
Dialog1.obj : error LNK2005: "public: int __thiscall testclass::geti ntfunction(void )" (?getintfunctio n@testclass@@QA EHXZ) already defined in lrsGlobalTest.o bj
Dialog1.obj : error LNK2005: "public: __thiscall testclass::~tes tclass(void)" (??1testclass@@ QAE@XZ) already defined in lrsGlobalTest.o bj
Debug/lrsGlobalTest.e xe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

Hmm...what do you think?
Jun 4 '07 #7
phenrol
8 New Member
Anyone have any more ideas on this?
Jun 4 '07 #8
vermarajeev
180 New Member
Anyone have any more ideas on this?
What weaknessforcats tells is correct. Declare your class object as extern.
Also when you use extern object in main.cpp dont #include header file of that class..

Might help....
Jun 5 '07 #9
phenrol
8 New Member
So far, none of this advice has helped much. I still get the same error I posted when I use extern. If I had another way to do this I would, but I don't unfortunately. Any help would be greatly appreciated.

Thanks
Jun 8 '07 #10

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

Similar topics

9
715
by: Paul | last post by:
Hi, VB.NET is saying the file I am creating is in use by another process and won't complete its task of moving the file to the specified destination folder. Here is my code (the main bit anyway).... Private Sub LogChange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs) If e.ChangeType = WatcherChangeTypes.Created Then
63
5932
by: Jerome | last post by:
Hi, I'm a bit confused ... when would I rather write an database application using MS Access and Visual Basic and when (and why) would I rather write it using Visual Studio .Net? Is it as easy in Visual Studio to create reports and labels as it's in Access?` The advantage of VS.net is that not every user needs Access, right? And that would eliminate the Access version problem as well I guess.
8
5481
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
6
4753
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much appreciated. Thanks in advance
6
3378
by: JonSteng | last post by:
..Net Visual Studio Professional 2003 Version 7.1.3088 ..Net Framework 1.1 SP1 Version 1.1.4322 IIS 5.1 Windows XP Professional SP2 Micron T3000 Laptop (1.5 GHz; 1GB RAM; 40GB HD with 17GB Free) I installed FrontPage server extensions to IIS on my computer while following instructions in a Microsoft ASP.Net MCSD training book. After installing the FrontPage Server Extensions I cannot create a new
34
2613
by: Mathieu Trentesaux | last post by:
Hello I downloaded Office 2007 for this reason : It seems, once again, that it is impossible to save any modification done in a VBA library, from the main project in Access. The save button remains desperatly grayed. It also seems impossible to open the library in another Access instance
18
9151
by: surfrat_ | last post by:
Hi, I am having the following problems in getting Microsoft Visual Studio 2005 Professional to link to an Access .mdb database. Please help me to sort this out. Problem 1: The Microsoft page "How to: Connect to Data in an Access Database"
3
3052
by: Ciegalo | last post by:
Hi to all, I'm getting my hands into PEAR for a small newsletter-sending project. I need to boost the performance of the sending script and came accross this mail_queue class that should queue the emails. After sweating over the pear installation on myWindows Machine (IIS, PHP 4.4.6, MySQL), I cannot even run the tutorial script.. I get : Fatal error: Cannot redeclare class mail_queue_container:mail_queue_container_db in...
3
2311
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure I was understanding the chapter on policy classes was to attempt to apply them to my project. I understand the general concept of policies but I lack the knowledge and wisdom of how to identify them in an existing project. So I figured to get an...
0
9589
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
10049
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...
0
9865
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
8873
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...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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
5309
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...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
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.