Variable array size in a class

Feb 8, 2017 at 12:39pm
Hi, I am wondering how to create a variable array called "ValueFilter", the variable size being defined using "LengthFilter".

Thanks in advance

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
class TheFilter {
public:
	TheFilter();
	~TheFilter();
	void EnterFilter();
	void ShowFilter();
	void Valid();
	void LoadFilter();
	void SaveFilter() const;
private:
	int LengthFilter;
	int ValueFilter[];
};

TheFilter::TheFilter() {
	LengthFilter = 0;
	ValueFilter = new int[LengthFilter];
}

TheFilter::~TheFilter() {
	cout << "Filter deleted" << endl;
}

void TheFilter::EnterFilter() {
	cout << "Please enter the number of values in the filter" << endl;
	cout << "-----------------------------------------------" << endl;
	cin >> LengthFilter;
	cout << "Please enter the values of the filter" << endl;
	cout << "-------------------------------------" << endl;
	cin >> ValueFilter[];
	cout << "Filter entered" << endl;
}
Feb 8, 2017 at 12:51pm
You either need to use the vector class, which is effectively an array replacement class, or a pointer.

the pointer looks like..

ctor
{
pointer = new type[variable];
}

dtor
{
delete[] pointer;
}


vectors take a few min to review before using, its not too hard but its a critical class for modern c++ programming.


so what you did looks like you were trying to use the pointer approach, but you missed.

it should be
int *ValueFilter;
instead of
int ValueFilter[];


and
LengthFilter = 0;
ValueFilter = new int[LengthFilter];

this isn't right. Don't set it to zero, it should be part of the user supplied data.

Last edited on Feb 8, 2017 at 12:54pm
Feb 8, 2017 at 12:52pm
use...vector ?
Feb 8, 2017 at 12:52pm
ValueFilter should be a pointer to int:
1
2
3
4
5
6
7
8
9
#include <iostream>

constexpr auto LengthFilter = 5;

int main()
{
    int* ValueFilter = new int[LengthFilter];
    delete [] ValueFilter;
}
Last edited on Feb 8, 2017 at 12:53pm
Feb 8, 2017 at 12:54pm
like this
1
2
3
4
vector <int> ValueFilter;
int LengthFilter;
cin >> LengthFilter;
ValueFilter.resize(LengthFilter);


here, if you dont know how to use vector..
http://www.cplusplus.com/reference/vector/
Feb 8, 2017 at 1:01pm
??

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
class TheFilter {
public:
	TheFilter();
	~TheFilter();
	void EnterFilter();
	void ShowFilter();
	void Valid();
	void LoadFilter();
	void SaveFilter() const;
private:
	int* ValueFilter;
};

TheFilter::TheFilter() {
	int* ValueFilter= new int[LenghtFilter];
}

TheFilter::~TheFilter() {
	cout << "Filter deleted" << endl;
}

void TheFilter::EnterFilter() {
	cout << "Please enter the number of values in the filter" << endl;
	cout << "-----------------------------------------------" << endl;
	cin >> LengthFilter;
	cout << "Please enter the values of the filter" << endl;
	cout << "-------------------------------------" << endl;
	cin >> ValueFilter[];
	cout << "Filter entered" << endl;
}
Last edited on Feb 8, 2017 at 1:02pm
Feb 8, 2017 at 1:18pm
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
class TheFilter {
public:
	TheFilter();
	~TheFilter();
	void EnterFilter();
	void ShowFilter();
	void Valid();
	void LoadFilter();
	void SaveFilter() const;
private:
	Vector <int> ValueFilter;
	int LengthFilter;
};

//TheFilter::TheFilter() {
//	int* ValueFilter= new int[LenghtFilter];
//}

TheFilter::~TheFilter() {
	cout << "Filter deleted" << endl;
}

void TheFilter::EnterFilter() {
	cout << "Please enter the number of values in the filter" << endl;
	cout << "-----------------------------------------------" << endl;
	cin >> LengthFilter;
	ValueFilter.resize(LengthFilter)
	cout << "Please enter the values of the filter" << endl;
	cout << "-------------------------------------" << endl;
	cin >> ValueFilter;
	cout << "Filter entered" << endl;
}
Feb 8, 2017 at 1:59pm
If acquiring resources through ctor then release these resources through the dtor:
1
2
3
4
5
6
7
8
TheFilter::TheFilter() {
	int* ValueFilter= new int[LenghtFilter];
}

TheFilter::~TheFilter() {
        delete [] ValueFilter;
	cout << "Filter deleted" << endl;
}
Last edited on Feb 8, 2017 at 2:00pm
Feb 8, 2017 at 4:51pm
"Vector" is an undeclared identifier?
Feb 8, 2017 at 5:25pm
"Vector" is an undeclared identifier?


Did you #include <vector> ?

If you didn't you have to use std::vector
Feb 8, 2017 at 7:03pm
"Vector" is an undeclared identifier?

Line 11: std::vector should not be capitalized.


Last edited on Feb 8, 2017 at 7:04pm
Feb 8, 2017 at 7:47pm

delete [] ValueFilter;


This line doesn't work, something to do with "delete" and "ValueFilter"

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
34
35
36
class TheFilter {
public:
	TheFilter();
	~TheFilter();
	void EnterFilter();
	void ShowFilter();
	void Valid();
	void LoadFilter();
	void SaveFilter() const;
private:
	int LengthFilter;
	vector <int> ValueFilter;
};

TheFilter::TheFilter() {
	int* ValueFilter = new int [LengthFilter];
}

TheFilter::~TheFilter() {
	delete [] ValueFilter;
	cout << "Filter deleted" << endl;
}

void TheFilter::EnterFilter() {
	cout << "Please enter the number of values in the filter" <<endl;
	cout << "-----------------------------------------------" <<endl;
	cin >> LengthFilter;
	cout << "Please enter the values of the filter" <<endl;
	cout << "-------------------------------------" <<endl;
	cin >> ValueFilter;
	cout << "Filter entered" <<endl;
}

void TheFilter::ShowFilter() {
	cout << "The filter is" << ValueFilter <<endl;
}
Feb 8, 2017 at 8:16pm
int* ValueFilter = new int [LengthFilter];

this is a local variable to the function, and it should be a class member.

move the
int *ValueFilter; up to the class.
Keep
ValueFilter = new int [LengthFilter];

in the constructor.
Feb 8, 2017 at 8:21pm
Like so?


1
2
3
4
5
private:
	int LengthFilter;
	vector <int> ValueFilter;
	int* ValueFilter;
};
Feb 8, 2017 at 8:38pm
No, you have 2 variables with the same name.

either use the vector,
or use the pointer.

not both.

If you are using vectors, do not do anything special in the constructor and destructor. You only need the allocation and deallocations for a pointer. Vector is a class that will handle its own memory stuff hidden from you.
Last edited on Feb 8, 2017 at 8:40pm
Feb 8, 2017 at 8:57pm
Better?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class TheFilter {
public:
	TheFilter();
	~TheFilter();
	void EnterFilter();
	void ShowFilter();
	void Valid();
	void LoadFilter();
	void SaveFilter() const;
private:
	int LengthFilter;
	vector <int> ValueFilter;
};

TheFilter::TheFilter() {
	 ValueFilter = new int [LengthFilter];
}

TheFilter::~TheFilter() {
	delete [] ValueFilter;
	cout << "Filter deleted" << endl;
}
Feb 8, 2017 at 9:12pm
neil222 wrote:
Better?
No.
As jonnin said,
jonnin wrote:
either use the vector,
or use the pointer.

not both.


If you use a vector, you don't need the variable LengthFilter either, because the vector maintains its own length - access it as ValueFilter.size() if necessary.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class TheFilter {
public:
	TheFilter();
	~TheFilter();
	void EnterFilter();
	void ShowFilter();
	void Valid();
	void LoadFilter();
	void SaveFilter() const;
private:
	vector <int> ValueFilter;
};

TheFilter::TheFilter() {
}

TheFilter::~TheFilter() {
	cout << "Filter deleted" << endl;
}
Feb 8, 2017 at 11:11pm
I thought the op wants the user to enter the length of the array(vector)
Feb 9, 2017 at 3:23am
still could, just check its size if a vector and if too big, complain or whatever.
Topic archived. No new replies allowed.