3

I have started programming with Java, I just achieved which I consider as a "good" level in matter of language knowledge.

For fun I decided to start programming using C++, I'm fairly new to this language but I'm a fast learner and I think it's not that far from Java.

I've created a test class which has a value and a name as attributes, and an objects counter as a global variable.

 #include<iostream>


/* local variable is same as a member's name */
class Test
{
    private:
       double x;
       std::string name;
       static int nb;
    public:
        Test(double x, std::string n)
        {
            this->x=x;
            this->name=n;
            nb=nb+1;
        }
       void setX (double x)
       {
           // The 'this' pointer is used to retrieve the object's x
           // hidden by the local variable 'x'
           this->x = x;
       }
       double getX()
       {
           return this->x;
       }
       std::string getName()
       {
           return this->name;
       }

       static int getNb()
       {
           return nb;
       }

};


int main()
{
   Test obj(3.141618, "Pi");
   std::cout<<obj.getX()<<" "<<obj.getName()<<" "<<Test::getNb()<<std::endl;
   return 0;
}

When executed the programm outputs this error :

In function `Test::Test(double, std::string)':
 (.text._ZN4TestC2EdSs[_ZN4TestC5EdSs]+0x4a): undefined reference to `Test::nb'
 (.text._ZN4TestC2EdSs[_ZN4TestC5EdSs]+0x53): undefined reference to `Test::nb'
In function `Test::getNb()':
 (.text._ZN4Test5getNbEv[_ZN4Test5getNbEv]+0x6): undefined reference to `Test::nb'
error: ld returned 1 exit status

some chinese to me.

I don't get it.

user253751
  • 57,427
  • 7
  • 48
  • 90
Aleks
  • 111
  • 2
  • 10
  • `Test(double x, std::string) { this->x=x; this->name=n; nb=nb+1; }` could be written more cleanly as `Test(double x_, std::string name_) : x(x_), name(name_) { ++nb; }`. Use initializers whenever you can for win and proffit. – kfsone Feb 22 '16 at 23:36
  • Good question. Code used could be shrunk down a lot, but otherwise brief and to the point. Can't hold the duplicate against them because this is hard to google until you know the keywords. – user4581301 Feb 22 '16 at 23:39

2 Answers2

4

In C++, static variables are essentially syntactic sugar around global variables. Just like global variables, they must be defined in exactly one source file, with:

int Test::nb;

and if you want to initialize it with a particular value,

int Test::nb = 5; // or some other expression
user253751
  • 57,427
  • 7
  • 48
  • 90
  • Thx a lot, I have already seen this but couldn't figure out the purpose, I should read about C struct to help understand. – Aleks Feb 22 '16 at 23:50
0

Your variable static int nb needs to be initialized so you need to add a declaration after the class.

class YourClass 
{
    // some stuff
}; //  Your class ends here
int Test::nb = 0;

int main() ...

Here's some tutorials and info tutorial point, cprogramming.com/tutorial/statickeyword

amisam
  • 91
  • 6
  • The definition (not "declaration" as you call it) should not be in a header file (your answer looks like you are suggesting to write it in the header just after the class definition) – M.M Feb 22 '16 at 23:47
  • @M.M Your right that the 'definition' should not be in a header file. I was assuming that this whole example was written in one main.cc file as the class definition is followed by the main function. – amisam Feb 22 '16 at 23:54