Good Afternoon,
I'm having a little trouble with a class. The error that I'm getting is: a nonstatic member reference must be relative to a specific object in the function void B::printStatic() { cout<<"size is "<<size<<endl; Here is the code that I have.
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
class B
{
private:
//const int cnt;
static int lifeCount;
static int deathCount;
const int size;
char *cat;
public:
B();
B(const int n);
void print();
static void printStatic();
~B();
};
int B::lifeCount=0;
int B::deathCount=0;
B::B():size(100)
{
lifeCount++;
cat = new char[size];
strcpy(cat, "I see a head initialization.");
}
B::B(const int n): size(n)
{
lifeCount++;
cat = new char[n];
if (n>50)
strcpy(cat, "Meow, meow, another head initialization.");
else
cat[0]='\0';
}
void B::print()
{
cout<<"size is => "<< size<<endl;
cout<<"cat is => " <<cat<<endl;
}
B::~B()
{
deathCount++;
printStatic();
delete[] cat;
}
void B::printStatic()
{
cout<<"size is " <<size<<endl;
cout<<lifeCount<<" many class B objects "
<<"have been created up to this point."<<endl;
cout<<deathCount<<" many class B objects have "
<<"died up to this point."<<endl;
}
int main()
{
return 0;
}