在方法中无效使用非静态数据成员

问题描述

我在第 159 行有这个错误

|159|error: invalid use of non-static data member 'TREE<T>::pl_last'
|13|note: declared here

这是一棵二叉树,它允许我计算对向量的一组连续元素进行特定操作的结果。执行我之前所说的函数中的错误。编写 Main 只是为了测试其余部分。

#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(),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,child2code(node),c2-i/2,c2);

            else
                return func(section(left,c1+i/2),section(left,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;
}

    

我不知道为什么。我有一些这个错误的实例,但它不适合在这里。是不是因为我在描述函数时使用了非静态值?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...