By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

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

P: 8
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.cpp is my main application source file and mysourcefile2.cpp is actually a dialog source file. The mysourcefile2.cpp 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
Share this Question
Share on Google+
9 Replies


gpraghuram
Expert 100+
P: 1,275
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

P: 8
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++\lrsGlobalTest\Dialog1.cpp(51) : error C2039: 'o_testclass' : is not a member of '`global namespace''
C:\admin\vc++\lrsGlobalTest\Dialog1.cpp(51) : error C2065: 'o_testclass' : undeclared identifier
C:\admin\vc++\lrsGlobalTest\Dialog1.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
Expert 100+
P: 1,764

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

P: 8
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
Expert Mod 5K+
P: 9,197
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.cpp. To use it, you meuch declare it in mysourcefile2.cpp.

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.cpp. So, you use the extern storage class specifer and declare o_testclass to be a testclass* that is external to mysourcefile2.cpp

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.cpp 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.cpp to the pointer o_testclass in mysourcefile1.cpp.
Jun 4 '07 #6

P: 8
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::testclass(void)" (??0testclass@@QAE@XZ) already defined in lrsGlobalTest.obj
Dialog1.obj : error LNK2005: "public: int __thiscall testclass::setintfunction(int)" (?setintfunction@testclass@@QAEHH@Z) already defined in lrsGlobalTest.obj
Dialog1.obj : error LNK2005: "public: int __thiscall testclass::getintfunction(void)" (?getintfunction@testclass@@QAEHXZ) already defined in lrsGlobalTest.obj
Dialog1.obj : error LNK2005: "public: __thiscall testclass::~testclass(void)" (??1testclass@@QAE@XZ) already defined in lrsGlobalTest.obj
Debug/lrsGlobalTest.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

Hmm...what do you think?
Jun 4 '07 #7

P: 8
Anyone have any more ideas on this?
Jun 4 '07 #8

100+
P: 180
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

P: 8
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

Post your reply

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