Issue:
I have a common question that I'm trying to solve, but I cannot figure out why the solution I have isn't working. I'm receiving a LNK2001 error message for not having numOfShapes properly defined. I think I have it defined, but the compiler thinks otherwise. What am I missing?
Context: I'm going through a tutorial, link with timestamp, on building a header file and a polymorphic header file when this error occurred. The variable isn't needed to making the program run, but I would like to know how to make it work for future use.
Research: This is a common issue and if you're looking for a similar, but different answer, then read this post. The linked post will probably solve your related problem. This has definitely helped me in understanding the problem I'm having, I just can't see why it doesn't work.
Header File: Shape.h
#ifndef SHAPE_H
#define SHAPE_H
// Header file
class Shape {
protected:
double height;
double width;
public:
static int numOfShapes;
Shape();
Shape(double length);
Shape(double height, double width);
virtual ~Shape();
void SetHeight(double height);
double GetHeight();
void SetWidth(double width);
double GetWidth();
static int GetNumOfShapes();
virtual double Area(); // Virtual allows for dependents to override the use of this function
};
#endif /* SHAPE_H */
Implementation File: Shape.cpp
#include "Shape.h"
// Impimentation file
// Shape Creation
Shape::Shape(double length) { this->height = length; this->width = length;}
Shape::Shape(double height, double width) { this->height = height; this->width = width;}
Shape::Shape() { this->height = 0; this->width = 0; }
// Shape Deletion
Shape::~Shape() = default;
// Get and Set variable inside of Shape Object
void Shape::SetHeight(double height) {this->height = height;}
double Shape::GetHeight() {return height;}
void Shape::SetWidth(double width) { this->width = width; }
double Shape::GetWidth() { return width;}
// Returning total number of shapes
int Shape::GetNumOfShapes() { return numOfShapes; } // <-- Source of Error Generation
// Calculating the area when called
double Shape::Area() {return height * width;}
// Default value
int numOfShapes = 0; // <-- Not defined here?