如何在C ++中创建指向矢量的静态指针?

时间:2019-02-18 03:02:14

标签: c++ static

如何在C ++中创建指向矢量的静态指针。我尝试过

// myfile.cpp
struct MyStruct
{
    static int x;
    static vector<int> *vec;
};

int MyStruct::x = 0;
vector<int> MyStruct::*vec = NULL;

int myfunc(vector<int> *nvec)
{
    static MyStruct *ms = NULL;
    if (ms == NULL)
    {
        ms->x = 7;
        ms->vec = nvec;
    }

    return 0;
}

但是我只得到undefined reference to MyStruct::vec

MyStruct::x可以正常工作。向量是怎么回事?

2 个答案:

答案 0 :(得分:3)

您需要使用:

vector<int>* MyStruct::vec = NULL;

对象类型为vector<int>*。该对象在MyStruct的范围内。


vector<int> MyStruct::*vec = NULL;

定义一个名为vec的全局变量,该变量可以指向类型为MyStruct的{​​{1}}的成员变量。

给予

vector<int>

您可以使用

MyStruct { vector<int> a; vector<int> b; };

vec = &MyStruct::a;

如您所见,那是完全不同的。

答案 1 :(得分:2)

该行必须为

def window_2():
    window_2 = tkinter.Tk()
    image2 = 'window2.gif'
    bg_image2 = tkinter.PhotoImage(file=image2)
    w = 1024
    h = 612
    ws = window_2.winfo_screenwidth()
    hs = window_2.winfo_screenheight()
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    window_2.geometry('%dx%d+%d+%d' % (w, h, x, y))
    panel2 =tkinter.Label(window_2, image=bg_image2)
    panel2.pack(side='top', fill='both', expand='yes')
    panel2.image = bg_image2
    window_2.title('Recipes')
    window_2.wm_iconbitmap('recipe.ico')

    def select():
    # optional
        selection = var.get()
        c.execute('SELECT * FROM Appetizers') # THIS ONLY PRINT THAT TABLE "Appetizer" BECAUSE IT IS NAMED, I NEED TO REPLACE THAT WITH THE VARIABLE FOR THE SELECTION.
        data = c.fetchall()
        print(data)

    var = tkinter.StringVar(window_2)
    var.set('Menu')

    choices = [
        'Appetizers',
        'Beef',
        'Bread',
        'Cake',
        'Chicken',
        'Chilli',
        'Curry',
        'Desert',
        'Drinks',
        'Egg',
        'Fish',
        'Pasta',
        'Pork',
        'Potato',
        'Rice',
        'Salad',
        'Sandwich',
        'Sauce',
        'Sea Food',
        'Slow Cooker',
        'Soup',
        'Stew',
        'Tofu',
        'Vegetables']

    option = tkinter.OptionMenu(window_2, var, *choices)
    option.place(x=215, y=120)

    button = tkinter.Button(window_2, text="check value selected", command=select)
    button.pack
    button.place(x=370, y=120)
相关问题