1

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?

G Beck
  • 73
  • 1
  • 12
  • 1
    Shape::nofshapes – pm100 Apr 25 '22 at 21:46
  • 1
    Does this answer your question? [Initialize static variables in C++ class?](https://stackoverflow.com/questions/5019856/initialize-static-variables-in-c-class) – Joseph Sible-Reinstate Monica Apr 25 '22 at 21:50
  • 1
    The last line in the linked tutorial is given as `int Shape::numOfShapes = 0;`. I am voting to close as typo. Without the `Shape::` part, the compiler can't know that you want to define the `numOfShapes` inside `Shape`. (It is the same syntax as for the definitions of the member functions.) – user17732522 Apr 25 '22 at 22:04
  • Thank you. That was definitely the source of my problem. – G Beck Apr 25 '22 at 22:08

0 Answers0