2017-08-06 62 views
0

我有一個簡單的C++應用程序。當我嘗試使用sizeof和一個成員變量時,我得到一個錯誤"Incomplete type is not allowed"。當我使全局變量(如在main之外定義它)我沒有得到這個錯誤。在C++中使用受保護的成員數組時不完整的類型

這是爲什麼?

在代碼中我的問題是這樣的:

class example : public application 
{ 
private: 
    void init() 
    { 
     // The "sizeof" call raises an error, "incomplete type is not allowed" 
     glNamedBufferStorage(vbo, sizeof(vertices), vertices, 0); 
    } 

    const GLfloat vertices[] = {1, 2, 3}; 
} 

如果我定義const GLfloat vertices[] = {1, 2, 3};外部類(使它全球)工作的。

const GLfloat vertices[] = {1, 2, 3}; 
class example : public application 
{ 
private: 
    void init() 
    { 
     // This works 
     glNamedBufferStorage(vbo, sizeof(vertices), vertices, 0); 
    } 
} 
+1

除了別的,受保護的數據是一個壞主意。 –

+0

如果您在類之外而不是內聯中定義「example :: init」,會發生什麼情況? – bjhend

回答

2

你需要指定的vertices的大小,當你把它聲明。您不能使用初始化程序來爲類中的空數組指定大小(語言規範部分dcl.init.aggr,第5節)。

const GLfloat vertices[3] = {1, 2, 3};