Trouble initilizing variable within constructor.



Main file:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Count.h"

using namespace std;

int main() {
	Count test1;
	test1.information();
	return 0;
}


Count.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include "Count.h"

int Count::count = 0;

void information(){
	cout << Count::count << endl;
};

void id(){
		cout << id1 << endl; // says id1 was not declared in this scope did you mean id?
	};

Count::Count() {// member id1 was not initialized in this constructor.
	int id1 = ++(Count::count); // warning: unused variable: 'id1'.
}

Count::~Count() {

}


Count.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#ifndef COUNT_H_
#define COUNT_H_

#include <iostream>

using namespace std;

class Count {
public:
	static int count;
	int id1;
public:
	void information();
	void id();
	Count();
	virtual ~Count();
private:

};

#endif /* COUNT_H_ */
Do you realize that you are creating another instance of an int named id1 inside your constructor that is different than the variable named id1 contained in the class (that warning is a definite hint as to the problem)?


1
2
3
4
5
Count::Count() {// member id1 was not initialized in this constructor.
	int id1 = ++(Count::count); // warning: unused variable: 'id1'.
        ^^^  // This means a definition, not an initialization/assignment.
}
 


Perhaps you should start using constructor initialization lists instead of assigning values in the constructor body.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef COUNT_H_
#define COUNT_H_

#include <iostream>

using namespace std;

class Count {
public:
	static int count{};  // <--- Either this.
	int id1{};
public:
	void information();
	void id();
	Count() : count(0),id1(0){}  // Or this.
	virtual ~Count();
private:

};

#endif /* COUNT_H_ */ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Count.h"

int Count::count = 0;

void information(){  // <--- Also needs qualified.
	cout << Count::count << endl;
};

Count::void id(){  // <--- Changed.
		cout << id1 << endl; // says id1 was not declared in this scope did you mean id?
	};

Count::Count() {// member id1 was not initialized in this constructor.
	int id1 = ++(Count::count); // warning: unused variable: 'id1'.
}

Count::~Count() {

}


1
2
3
4
Count::Count()
{  // member id1 was not initialized in this constructor.
	int id1 = ++(Count::count); // warning: unused variable: 'id1'.
}

The variable "id1" is already a class member variable. In this function "id1" is a local variable to the function and is not used. I think what you want her is just "id1++;" to add 1 to the class variable.

Andy

Edit.
Last edited on
Without using inline:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using std::cout;

class Count {
	static int count;
	int id1 {};

public:
	void information();
	void id();
	Count();
	virtual ~Count();
};

int Count::count = 0;

void Count::information() {
	cout << Count::count << '\n';
};

void Count::id() {
	cout << id1 << '\n';
};

Count::Count() : id1(++Count::count) {}

Count::~Count() { }

int main() {
	Count test1;

	test1.information();
}

Thank you both for your replies.
Topic archived. No new replies allowed.