473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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
9 2203
gpraghuram
1,275 Expert 1GB
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
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
1,764 Expert 1GB

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
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 Expert Mod 8TB
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
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
Anyone have any more ideas on this?
Jun 4 '07 #8
vermarajeev
180 100+
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
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
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...
63
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...
8
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 -------------------------------------------------------------------------------- ...
6
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...
6
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)...
34
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...
18
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...
3
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...
3
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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,...

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.