Topic: static int" in class won't work


Author: ci1bfp@sunc.sheffield.ac.uk (B Pickford)
Date: 3 Dec 1993 14:17:53 GMT
Raw View
Hi Any & All,
I am having a problem with a C++ tutorial, the following code creates an
error whilst linking, ie it compiles fine.

The problem lies, so I am told in the fact there is no definition for the
static int in the global arena, however I cannot find a suitable position
for the definition that both the compiler and linker will accept.

I am using BC3.0 within the IDE, so if any of you would care to help me out of
this hole I would be eternally grateful.

The code in it's original form follows.
Many Thanks.

TTFN Fraz

                                 // Chapter 6 - Program 1
#include "iostream.h"

class box {
   int length;
   int width;
   static int extra_data;
public:
   box(void);             //Constructor
   void set(int new_length, int new_width);
   int get_area(void);
   int get_extra(void) {return(extra_data++);}
};


box::box(void)        //Constructor implementation
{
   length = 8;
   width = 8;
   extra_data = 1;
}


// This method will set a box size to the two input parameters
void box::set(int new_length, int nev_width)
{
   length = new_length;
   width = new_width;
}


// This method will calculate and return the area of a box instance
int box::get_area(void)
{
   return (length * width);
}


main()
{
box small, medium, large, group[4];        //Seven boxes to work with

   small.set(5, 7);
   large.set(15, 20);

   for (int index = 1 ; index < 4 ; index++)  //group[0] uses default
      group[index].set(index + 10, 10);

   cout << "The small box area is " << small.get_area() << "\n";
   cout << "The medium box area is " << medium.get_area() << "\n";
   cout << "The large box area is " << large.get_area() << "\n";

   for (index = 0 ; index < 4 ; index++)
      cout << "The array box area is " <<
                                     group[index].get_area() << "\n";

   cout << "The extra data value is " << small.get_extra() << "\n";
   cout << "The extra data value is " << medium.get_extra() << "\n";
   cout << "The extra data value is " << large.get_extra() << "\n";
   cout << "The extra data value is " << group[0].get_extra() << "\n";
   cout << "The extra data value is " << group[3].get_extra() << "\n";
}




// Result of execution
//
// The small box area is 35
// The medium box area is 64
// The large box area is 300
// The array box area is 64
// The array box area is 110
// The array box area is 120
// The array box area is 130
// The extra data value is 1
// The extra data value is 2
// The extra data value is 3
// The extra data value is 4
// The extra data value is 5