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.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
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 -
#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++\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
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
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.c pp. To use it, you meuch declare it in mysourcefile2.c pp. -
//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.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: -
//mysourcefile1.cpp
-
testclass* o_testclass = new testclass
-
-
//mysourcefile2.cpp
-
extern testclass* o_testclass;
-
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.
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?
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 anyway)....
Private Sub LogChange(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
If e.ChangeType = WatcherChangeTypes.Created Then
|
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.
|
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
|
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
|
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
| |
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
|
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"
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
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
| |
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...
| |