Variable array size in a class

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;
}
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
use...vector ?
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
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/
??

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
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;
}
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
"Vector" is an undeclared identifier?
"Vector" is an undeclared identifier?


Did you #include <vector> ?

If you didn't you have to use std::vector
"Vector" is an undeclared identifier?

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


Last edited on

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;
}
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.
Like so?


1
2
3
4
5
private:
	int LengthFilter;
	vector <int> ValueFilter;
	int* ValueFilter;
};
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
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;
}
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;
}
I thought the op wants the user to enter the length of the array(vector)
still could, just check its size if a vector and if too big, complain or whatever.
Topic archived. No new replies allowed.