-7

I want to increment a global variable inside a c++ class but can not configure out how to do so

int line = 0;
int ivar = 0;
int cvar = 0;
int svar = 0;

class file {
protected:
    char name[50];
    string read;
    string write;
    ifstream readfile;
    ofstream writefile;
    int i;
    int j;
};

I want to increment the cvar variable inside the class file. Something like

class file {
protected:
    char name[50];  cvar++;
    string read;
    string write;
    ifstream readfile;
    ofstream writefile;
    int i;
    int j;
};

But the compiler gives error if done this way. Can someone devise an alternative method to do so?

Zainzam
  • 1
  • 3
  • 6
    You can increment it inside of class method. Also global variables are not good. – user7860670 Jul 10 '17 at 11:23
  • 2
    You cannot put arbitrary code in a class declaration. As for proposing alternatives, we would have to know what you're trying to achieve. (P.S: `ivar`, `cvar` and `svar` are bad variable names) – Borgleader Jul 10 '17 at 11:25
  • _But the compiler gives error if done this way._ Did you try.. I don't know.. Reading the error? That may have given you a clue on what you were doing wrong. – Algirdas Preidžius Jul 10 '17 at 11:25
  • @AlgirdasPreidžius the error says says that "this declaration has no class or type specifier". – Zainzam Jul 10 '17 at 11:30
  • @Borgleader I am trying to add a counter to my program that counts the number of character type variables declared in the complete program. – Zainzam Jul 10 '17 at 11:32
  • @Zainzam Hence, you should've concluded that compiler was expecting a declaration. `cvar++` is not a declaration. You should read a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) if you are having these kinds of problems. – Algirdas Preidžius Jul 10 '17 at 11:35
  • @Zainzam you can't do this. BTW why do you want to do this ? It doesn't make much sense. Sounds like an [XY Problem](http://xyproblem.info/) – Jabberwocky Jul 10 '17 at 11:36

3 Answers3

2

You can, but the incrementation needs to be in a method of the class.

Using the default constructor would work, and this has the bonus that the incrementation happens when you create a new instance of your class:

file::file() /*ToDo - declare this in the class definition*/
{
    cvar++;
}

Finally, it would be a good idea to use std::atomic<int> as the type for cvar, in order to prevent concurrency issues (namely simultaneous read and writes on cvar).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

You can't put lines of code to be executed in a class definition. They need to be in a function.

Perhaps in your case, a constructor:

class file {
public:
    file() {
        cvar++;
    }

protected:
    char name[50];  
    string read;
    string write;
    ifstream readfile;
    ofstream writefile;
    int i;
    int j;
};

There are several better approaches than global variables - which depend on what you are trying to achieve.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

From your comment:

I am trying to add a counter to my program that counts the number of character type variables declared in the complete program.

Global variables are not nice. Also "manually" incrementing a counter whenever you use a char[] isnt nice either. I would probably use something similar to this:

struct MyString {
    std::string theString;
    static int counter;
    MyString() { counter++;}
};

int MyString::counter = 0;

and then

struct file {
    MyString name;
    /*...*/
};

The static variable acts more or less like a global, but it doest pollute your namespace, because it is where it belongs (MyString::counter). Further, if you use MyString you have no chance to forget incrementing the counter each time you need a string. Last but not least I changed from char[] to std::string, because unless there is a reason std::string should be prefered over char[].

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185