mickey.marshall@litepoint.com wrote:
Quote:
On May 15, 11:35 am, Zeppe
<zeppe@.remove.all.this.long.comment.email.itwrote :
Quote:
>mickey.marsh...@litepoint.com wrote:
Quote:
>>What am I doing wrong?
>>
>you are not posting the code that doesn't work.
>>
>Regards,
>>
>Zeppe
>
Here is the sample code:
Main Code
#include "stdafx.h"
#include "test.h"
#include "testDlg.h"
#include ".\testdlg.h"
#include ".\arithmetic.h"
>
.
.
.
void CtestDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
int rv;
InitAB();
CAddTwoNumbers Add2;
//CSubtract Sub2;
Arithmetic *ar1=&Add2;
//Arithmetic *ar2=&Sub2;
//ar= new Arithmetic;
ar1->EnterTwoNumbers(2,4);
//ar2->EnterTwoNumbers(8,4);
rv=Add2.Add ();
//rv=Sub2.Sub2 ();
>
}
>
Globals.h
#ifndef MYGLOBALS_H
#define MYGLOBALS_H
static int *a1; //this is the global variable
static int *b1; //this is the global variable
The comments are incorrect. Those are not global variables.
Those are file-scoped variables living outside of any function.
The main thing here is to understand that there is a copy of
each in each tranlsation unit.
Replace the word 'static' with the word 'extern', and you get
closer to the truth. You will still need to _define_ those in
some (only one) translation unit before attempting to use them.
I would put the definitions in the same file where 'InitAB' is.
Quote:
void InitAB();
#endif
>
globals.cpp
#include ".\globals.h"
Replace \ with /.
Add here:
int *a1 = 0;
int *b1 = 0;
(those are the definitions)
Quote:
void InitAB()
{
a1=new int;
b1=new int;
}
>
Master class (arithmetic.cpp)
#include "StdAfx.h"
#include ".\arithmetic.h"
//#include ".\globals.h"
//extern int a1,b1;
Arithmetic::Arithmetic(void)
{
}
>
Arithmetic::~Arithmetic(void)
{
}
void Arithmetic::EnterTwoNumbers(int a, int b)// this works fine
{
*a1=a;
*b1=b;
>
}
void Arithmetic::GetTwoNumbers(int * a, int * b)
{
if (NULL!=a)
if (NULL!=b)
{
*a=*a1;
*b=*b1;
}
}
>
inherited class
>
#include "StdAfx.h"
#include ".\arithmetic.h"
//#include ".\globals.h"
//extern int a1,b1;
>
CAddTwoNumbers::CAddTwoNumbers(void)
{
}
>
CAddTwoNumbers::~CAddTwoNumbers(void)
{
}
CAddTwoNumbers::Add(void)// a1 and b1 are both NULL here
{
return(*a1+*b1);
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask