I'm trying to make automatic generation of a value in a class in C++.
This is Shape.h
#pragma once
class Shape
{
public:
Shape();
~Shape();
// Return ID of the Shape
virtual int getId();
// Return area of the Shape
virtual double getArea();
protected:
static int _id;
double _area;
// Compute the area of the shape
virtual void compArea() = 0;
};
And this is Shape.cpp
#include <iostream>
#include "Shape.h"
Shape::Shape()
{
_id++; //ID is incremented in every instance
_area = 0.0;
}
Shape::~Shape()
{
std::cout << "Distruttore Shape" << std::endl;
}
// Return ID of the Shape
int Shape::getId()
{
return _id;
}
// Return area of the Shape
double Shape::getArea()
{
return _area;
}
And when I compile I receive:
error LNK2001: external symbol "public: static int Shape::_id" (?_id@Shape@@2HA) unresolved
What am I doing wrong?
Thanks for help :)