1
#include <iostream>
using namespace std;

class Assn2
{
  public:
     static void set_numberofshape();
     static void increase_numberofshape();

  private:         
      static int numberofshape22;
};

void Assn2::increase_numberofshape()
{
  numberofshape22++;
}

void Assn2::set_numberofshape()
{
  numberofshape22=0;
} // there is a problem with my static function declaration

int main()
{
  Assn2::set_numberofshape();
}

Why do I get an error undefined reference to Assn2::numberofshape22 when I compile this?

I am trying to declare an static integer :numberofshape22 and two methods.

Method 1 increase numberofshapes22 by 1

Method 2 initialise numberofshape22 to 0

What am I doing wrong ??

2 Answers 2

4

You just declared the variable. You need to define it:

int Assn2::numberofshape22;
Sign up to request clarification or add additional context in comments.

Comments

2

The declaration of a static data member in the member list of a class is not a definition.

To respect the one Definition Rule you must define a static data member. In your case you only declared it.

Example:

// in assn2.h
class Assn2
{
  // ...
  private:         
      static int numberofshape22; // declaration
};

// in assn2.cpp

int Assn2::numberofshape22; // Definition

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.