-4

I can't understand why static private variable is not working in my code! Here is the code:

#include<iostream>
#include<string>
using namespace std;
class KittyCat{
private:
int a;
string t;
static int count;
public:
KittyCat(){
    a=0;
    t="NULL";
    count++;
}
void set(int i, string m){
    a=i;
    t=m;
}
void show(){
    cout << "A is: "<< a <<" T is: " << t <<"\n\n";
}
void totalCount(){
    cout <<"Total Counts: "<< count <<"\n\n";
}

};
void main(){
KittyCat tech, review, article, photo, video;
tech.set(10, "Technology");
review.set(85, "Reviews");
article.set(54, "Articles");
article.show();
article.totalCount();
}

Any ideas?

Ravid Goldenberg
  • 2,119
  • 4
  • 39
  • 59
Jessica Lingmn
  • 1,162
  • 3
  • 10
  • 15
  • Do public static variables work for you when used like this? – juanchopanza Jun 26 '13 at 05:48
  • you first need to initialize the static variable. int KittyCat::count=0; – Navin Jun 26 '13 at 05:49
  • 6
    Instead of (or in addition to) "not working", it's better to say what behavior you expected, and what behavior you observed. – ruakh Jun 26 '13 at 05:50
  • 1
    @Navin, no, the 'count' is initialized to 0 by default. – user1764961 Jun 26 '13 at 05:54
  • 1
    You've *declared* `count`, but you've never *defined* it. Add a line like `int KittyCat::count;` after the definition of `KittCat`, but before `main`. – Jerry Coffin Jun 26 '13 at 05:59
  • @user1764961 not initialized but define. like Jerry Coffin mentioned – Navin Jun 26 '13 at 06:01
  • 1
    @Navin, for the second time, the static variable is initialized to 0 by default! – user1764961 Jun 26 '13 at 06:04
  • 2
    @user1764961 thats what i said... the word "initialize" i mentioned is wrong and it should be "define". Like jerry's examples. – Navin Jun 26 '13 at 06:07
  • @Navin, no offense, but based on what you wrote, it seems like you still think it's necessary to explicitly assign zero to the 'count'. And that is NOT true. – user1764961 Jun 26 '13 at 06:10
  • What I meant by my first comment is, if you think `private` has anything to do with it, try `public`. That will help you narrow down your problem. – juanchopanza Jun 26 '13 at 06:13
  • 1
    @user1764961 yes it's not necessary to initialize it to 0, my mistake. but definitely need to define the static variable. in my first comment i defined and initialized it to 0 because it's a good practice. – Navin Jun 26 '13 at 06:15

3 Answers3

0

Even int KittyCat::count; would have also worked. The default value of a static data member is 0. A static data member is declared within the class but defined outside the class definition.

-1
int KittyCat::count = 0;

must be added before main,because all static data member must be initialized before use.

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
  • 1
    Initializing is a good idea, but given the discussion in the comments to the question, you could have thought about this answer a bit more. – jogojapan Jun 26 '13 at 06:25
-3

Got this:

#include<iostream>
#include<string>
using namespace std;
class KittyCat{
private:
int a;
string t;
static int count;
public:
KittyCat(){
    a=0;
    t="NULL";
    count++;
}
void set(int i, string m){
    a=i;
    t=m;
}
void show(){
    cout << "A is: "<< a <<" T is: " << t <<"\n\n";
}
void totalCount(){
    cout <<"Total Counts: "<< count <<"\n\n";
}

};
int KittyCat::count=0;
void main(){
KittyCat tech, review, article, photo, video;
tech.set(10, "Technology");
review.set(85, "Reviews");
article.set(54, "Articles");
article.totalCount();
}
Jessica Lingmn
  • 1,162
  • 3
  • 10
  • 15