Problem with Pointer simulation
I have just made a program to simulate the pointers, and I have problem with it
Here is the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#pragma once
#include<iostream>
using namespace std;
class Float
{
protected:
static int fmem_top;
static float fmemory[50];
int addr;
public:
Float(void);
Float(float);
int operator&();
~Float(void);
};
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include "Float.h"
int Float::fmem_top = 0;
Float::Float(void){}
Float::Float(float s)
{
fmemory[fmem_top] = s;
addr = fmem_top;
fmem_top ++;
}
int Float::operator&()
{
return addr;
}
Float::~Float(void)
{
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#pragma once
#include"Float.h"
class ptrFloat : public Float
{
protected:
int addrs;
static int count;
static int pmemory[50];
public:
ptrFloat(int);
float& operator*();
~ptrFloat(void);
};
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include "ptrFloat.h"
int ptrFloat::count = 0;
ptrFloat::ptrFloat(int x)
{
pmemory[count] = x;
addrs = count;
count++;
}
float& ptrFloat::operator*()
{
return fmemory[ pmemory[addrs] ];
}
ptrFloat::~ptrFloat(void)
{
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include"Float.h"
#include"ptrFloat.h"
int main()
{
Float var1 = 1.234; // define and initialize two Floats
Float var2 = 5.678;
ptrFloat ptr1 = &var1; // define two pointers-to-Floats,
ptrFloat ptr2 = &var2; // initialize to addresses of Floats
cout << " *ptr1=" << *ptr1; // get values of Floats indirectly
cout << " *ptr2=" << *ptr2; // and display them
/**ptr1 = 7.123; // assign new values to variables
*ptr2 = 8.456;
cout << " *ptr1=" << *ptr1; // get new values indirectly
cout << " *ptr2=" << *ptr2; // and display them*/
return 0;
}
|
And here is the Errors.
1>Float.obj : error LNK2001: unresolved external symbol "protected: static float * Float::fmemory" (?fmemory@Float@@1PAMA)
1>ptrFloat.obj : error LNK2001: unresolved external symbol "protected: static float * Float::fmemory" (?fmemory@Float@@1PAMA)
1>ptrFloat.obj : error LNK2001: unresolved external symbol "protected: static int * ptrFloat::pmemory" (?pmemory@ptrFloat@@1PAHA)
1>C:\Users\F.B.M\Desktop\Pointer-12\Debug\Pointer-12.exe : fatal error LNK1120: 2 unresolved externals |
Please help
Thanks.
class member that are declared as static must be defined outside the class.
For class Float, the following definition are required at file scope (in Float.cpp)
1 2
|
int Float::fmem_top;
float Float::fmemory[50];
|
And similarly for class ptrFloat.
Andy
P.S. I see you've done the right thing for some but not all of your statics!
Last edited on
Thank you very much (andywestken), that solved the problem.
Thanks again.
Topic archived. No new replies allowed.