Feb 8, 2017 at 12:51pm UTC
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 UTC
Feb 8, 2017 at 12:52pm UTC
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 UTC
Feb 8, 2017 at 1:01pm UTC
??
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 UTC
Feb 8, 2017 at 1:59pm UTC
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 UTC
Feb 8, 2017 at 4:51pm UTC
"Vector" is an undeclared identifier?
Feb 8, 2017 at 7:03pm UTC
"Vector" is an undeclared identifier?
Line 11: std::vector should not be capitalized.
Last edited on Feb 8, 2017 at 7:04pm UTC
Feb 8, 2017 at 8:16pm UTC
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:38pm UTC
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 UTC
Feb 8, 2017 at 11:11pm UTC
I thought the op wants the user to enter the length of the array(vector)
Feb 9, 2017 at 3:23am UTC
still could, just check its size if a vector and if too big, complain or whatever.