I have this error in 159th line.
|159|error: invalid use of non-static data member 'TREE<T>::pl_last'
|13|note: declared here
It's a binary tree which would allow me to calculate the outcome of a specific operation on a set of consecutive elements of a vector. The error in the function performing what I said earlier. Main was written only for the sake of testing the rest.
#include <iostream>
#include <vector>
#include <functional>
#include <cmath>
using namespace std;
template <class T>
class TREE
{
private:
int last, pl_last;
T neut;
vector <T> con;
function<T(const T&, const T&)> func;
int mnwipo2(int s)
{
int i;
for (i = 1; i < s; i*=2){}
return i;
}
void refresh()
{
for (int i = con.size()/2 - 1; i >=0; i--)
{
con[i]=func(child1(i), child2(i));
}
}
int parentcode (int c)
{
return (c-1)/2;
}
int child1code (int p)
{
return 2*p+1;
}
int child2code (int p)
{
return 2*p+2;
}
void refresh_last()
{
int i = last;
do
{
i = parentcode(i);
con[i]=func(child1(i), child2(i));
}
while (parentcode(i) != i);
}
public:
TREE(T neutral, function<T(const T&, const T&)> f) : func(f){last = -1; neut = neutral;}
T parent (int c)
{
return con[(c-1)/2];
}
T child1 (int p)
{
return con[2*p+1];
}
T child2 (int p)
{
return con[2*p+2];
}
void add (T dod)
{
int s = con.size();
if (last == (s-1))
{
con.push_back(dod);
int pom = (s+1)/2;
for (int i = 0; i < pom; i++)
{
con.insert(con.begin(), neut);
}
for (int i = 2; i < pom; i++)
{
con.push_back(neut);
}
//con.insert(con.end(), (s+1)/2, neutral);
//con.insert(con.begin(), (s+1)/2, neutral);
refresh();
pl_last = 2*s;
last = pl_last - pom + 1;
cout << endl << last << " " << pl_last <<" "<< con.size() << endl;
}
else
{
con [++last] = dod;
refresh_last();
}
}
void cr_on_base (vector <T> & V)
{
con.clear();
int s = V.size();
int pom = mnwipo2(s);
pl_last = 2*pom-2;
for (int i = 1; i < pom; i++)
{
con.push_back(0);
}
con.insert(con.end(), V.begin(), V.end());
for (int i = con.size(); i <= pl_last; i++)
{
con.push_back(0);
}
refresh();
last = (pom -2 + s);
}
void show()
{
int i = 0, j, p, nr_pot = 0;
while (i < con.size())
{
p = pow(2, nr_pot);
for (j = 0; j < p; j++)
{
cout << con[i] << " ";
i++;
}
cout << endl;
nr_pot ++;
}
}
T operator[](int i)
{
return con[i];
}
size()
{
return con.size();
}
T section (int left, int right, int node = 0, int c1 = 0, int c2 = (pl_last)/2) // error here
{
if (left == c1 && right == c2)
return con[node];
int i = c2-c1;
if (right <= c1 + i/2)
return section(left, right, child1code(node), c1, c1+i/2);
else if (left >= c2 - i/2)
return section(left, right, child2code(node), c2-i/2, c2);
else
return func(section(left, right, child1code(node), c1, c1+i/2), section(left, right, child2code(node), c2-i/2, c2));
}
};
float binarne (float a, float b)
{
return a+b;
}
int main()
{
TREE <double> drzewo (0, binarne);
double d;
int i, n;
cin >> n;
vector <double> V (n);
for (i = 0; i < n; i++)
cin >> V[i];
drzewo.cr_on_base(V);
drzewo.show();
cin >> d;
drzewo.add(d);
drzewo.show();
return 0;
}
I don't know why. I some instances of this error, but it doesn't fit in here. Is it because I used a non-static value in describtion of a function?