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: -
//myclass.h file
-
class testclass
-
{
-
public:
-
testclass::testclass(void);
-
int setintfunction(int testinteger);
-
int getintfunction(void);
-
testclass::~testclass(voide);
-
private:
-
int p_testinteger
-
};
-
-
testclass::testclass(void)
-
{
-
p_testinteger = 0
-
}
-
-
int testclass::setintfunction(int testinteger);
-
{
-
p_testinteger = testinteger;
-
return 0;
-
}
-
-
int testclass::getintfunction(void);
-
{
-
return p_testinteger;
-
}
-
-
testclass::~testclass(void);
-
{
-
}
-
-
//mysourcefile1.cpp
-
testclass* o_testclass = new testclass
-
int main
-
{
-
::o_testclass->setintfunction(13);
-
}
-
-
-
//mysourcefile2.cpp
-
int testfunction
-
{
-
printf("%d",::o_testclass->getintfunction());
-
}
-
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
9 2203
Hi,
There are couple of comilation errors in your code like jmissing semicolons and typo errors.
I have corrected the same .Try this -
#include<stdio.h>
-
-
class testclass
-
{
-
public:
-
testclass::testclass(void);
-
int setintfunction(int testinteger);
-
int getintfunction(void);
-
~testclass(void);
-
private:
-
int p_testinteger;
-
};
-
-
testclass::testclass(void)
-
{
-
p_testinteger = 0;
-
}
-
-
int testclass::setintfunction(int testinteger)
-
{
-
p_testinteger = testinteger;
-
return 0;
-
}
-
-
int testclass::getintfunction(void)
-
{
-
return p_testinteger;
-
}
-
-
testclass::~testclass(void)
-
{
-
}
-
-
//mysourcefile1.cpp
-
testclass* o_testclass = new testclass;
-
int main()
-
{
-
::o_testclass->setintfunction(13);
-
}
-
-
-
//mysourcefile2.cpp
-
int testfunction()
-
{
-
printf("%d",::o_testclass->getintfunction());
-
}
-
Raghuram
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: -
//testclass.h file
-
class testclass
-
{
-
public:
-
testclass::testclass(void);
-
int setintfunction(int testinteger);
-
int getintfunction(void);
-
~testclass(void);
-
private:
-
int p_testinteger;
-
};
-
-
testclass::testclass(void)
-
{
-
p_testinteger = 0;
-
}
-
-
int testclass::setintfunction(int testinteger)
-
{
-
p_testinteger = testinteger;
-
return 0;
-
}
-
-
int testclass::getintfunction(void)
-
{
-
return p_testinteger;
-
}
-
-
testclass::~testclass(void)
-
{
-
}
-
-
//Dialog1.h file
-
#if !defined(AFX_DIALOG1_H__96E75EE7_F10A_44B0_8B55_D525D4FD4519__INCLUDED_)
-
#define AFX_DIALOG1_H__96E75EE7_F10A_44B0_8B55_D525D4FD4519__INCLUDED_
-
-
#if _MSC_VER > 1000
-
#pragma once
-
#endif // _MSC_VER > 1000
-
// Dialog1.h : header file
-
//
-
-
/////////////////////////////////////////////////////////////////////////////
-
// CDialog1 dialog
-
-
class CDialog1 : public CDialog
-
{
-
// Construction
-
public:
-
CDialog1(CWnd* pParent = NULL); // standard constructor
-
-
// Dialog Data
-
//{{AFX_DATA(CDialog1)
-
enum { IDD = IDD_DIALOG1 };
-
CEdit m_edit;
-
//}}AFX_DATA
-
-
-
// Overrides
-
// ClassWizard generated virtual function overrides
-
//{{AFX_VIRTUAL(CDialog1)
-
protected:
-
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
-
//}}AFX_VIRTUAL
-
-
// Implementation
-
protected:
-
-
// Generated message map functions
-
//{{AFX_MSG(CDialog1)
-
afx_msg void OnButton1();
-
//}}AFX_MSG
-
DECLARE_MESSAGE_MAP()
-
};
-
-
//{{AFX_INSERT_LOCATION}}
-
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
-
-
#endif // !defined(AFX_DIALOG1_H__96E75EE7_F10A_44B0_8B55_D525D4FD4519__INCLUDED_)
-
-
-
-
//lrsGlobalTest.cpp file
-
#include "stdafx.h"
-
#include "lrsGlobalTest.h"
-
#include "testClass.h"
-
#include "Dialog1.h"
-
-
#ifdef _DEBUG
-
#define new DEBUG_NEW
-
#undef THIS_FILE
-
static char THIS_FILE[] = __FILE__;
-
#endif
-
testclass* o_testclass = new testclass;
-
/////////////////////////////////////////////////////////////////////////////
-
// The one and only application object
-
-
CWinApp theApp;
-
-
using namespace std;
-
-
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
-
{
-
int nRetCode = 0;
-
-
// initialize MFC and print and error on failure
-
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
-
{
-
// TODO: change error code to suit your needs
-
cerr << _T("Fatal Error: MFC initialization failed") << endl;
-
nRetCode = 1;
-
}
-
else
-
{
-
// TODO: code your application's behavior here.
-
CString strHello;
-
strHello.LoadString(IDS_HELLO);
-
cout << (LPCTSTR)strHello << endl;
-
::o_testclass->setintfunction(13);
-
printf("%d",::o_testclass->getintfunction());
-
CDialog1 dlg;
-
dlg.DoModal();
-
-
}
-
-
return nRetCode;
-
}
-
-
//Dialog1.cpp file
-
#include "stdafx.h"
-
#include "lrsGlobalTest.h"
-
#include "Dialog1.h"
-
-
#ifdef _DEBUG
-
#define new DEBUG_NEW
-
#undef THIS_FILE
-
static char THIS_FILE[] = __FILE__;
-
#endif
-
-
/////////////////////////////////////////////////////////////////////////////
-
// CDialog1 dialog
-
-
-
CDialog1::CDialog1(CWnd* pParent /*=NULL*/)
-
: CDialog(CDialog1::IDD, pParent)
-
{
-
//{{AFX_DATA_INIT(CDialog1)
-
// NOTE: the ClassWizard will add member initialization here
-
//}}AFX_DATA_INIT
-
//AfxMessageBox("test");
-
}
-
-
-
void CDialog1::DoDataExchange(CDataExchange* pDX)
-
{
-
CDialog::DoDataExchange(pDX);
-
//{{AFX_DATA_MAP(CDialog1)
-
DDX_Control(pDX, IDC_EDIT1, m_edit);
-
//}}AFX_DATA_MAP
-
}
-
-
-
BEGIN_MESSAGE_MAP(CDialog1, CDialog)
-
//{{AFX_MSG_MAP(CDialog1)
-
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
-
//}}AFX_MSG_MAP
-
END_MESSAGE_MAP()
-
-
/////////////////////////////////////////////////////////////////////////////
-
// CDialog1 message handlers
-
-
void CDialog1::OnButton1()
-
{
-
// TODO: Add your control notification handler code here
-
int testint;
-
char teststr[3];
-
sprintf(teststr, "%d", ::o_testclass->getintfunction());
-
m_edit.SetWindowText(teststr);
-
}
-
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
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
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.
Here is your situation: -
//mysourcefile1.cpp
-
testclass* o_testclass = new testclass
-
-
//mysourcefile2.cpp
-
As you can see o_testclass is not in mysourcefile2.cpp. To use it, you meuch declare it in mysourcefile2.cpp. -
//mysourcefile1.cpp
-
testclass* o_testclass = new testclass
-
-
//mysourcefile2.cpp
-
testclass* o_testclass = new testclass
-
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: -
//mysourcefile1.cpp
-
testclass* o_testclass = new testclass
-
-
//mysourcefile2.cpp
-
extern testclass* o_testclass;
-
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.
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?
Anyone have any more ideas on this?
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....
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
--------------------------------------------------------------------------------
...
|
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...
|
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)...
|
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...
|
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...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |