2017-04-03 28 views
0

我正在嘗試使用JUCE Demo,並將BinaryData.cppOpenGLDemo.cpp的部分抽取到我自己的類中。不幸的是,我遇到一個問題我真的不能解析,我已經成功的三個文件這個小例子,以減少:main.cppmystuff.cppmystuff.h無效的非靜態數據成員用於訪問結構中的陣列

main.cpp

// g++ -std=c++11 main.cpp mystuff.cpp -o main 

#include "mystuff.h" 

int main(int argc, char* argv[]) 
{ 
    MyStuff tmpstuff; 
    std::cout << "hello world" << tmpstuff.temp_binary_data_7[0] << std::endl ; 
} 

mystuff.h

#include <iostream> 

class MyStuff 
{ 
public: 
    MyStuff(); 
    ~MyStuff(); 

    // from BinaryData.cpp: 
    //~ static const unsigned char temp_binary_data_7[] = 
    //~ { 35,32,77,97,120,50,79,98,106,32,86,101,114,115,105,111,110,32,52,46,48,32,77,97,114,32,49,48,116,104,44,32,50,48,48,49,10,35,10,35,32,111,98,106,101,99,116,32,84,101,97,112,111,116,48,49,32,116,111,32,99,111,109,101,32,46,46,46,10,35,10,118,32,32,53, 
    //~ 46,57,50,57,54,56,56,32,52,46,49,50,53,48,48,48,32,48,46,48,48,48,48,48,48,10,118,32,32,53,46,56,51,50,48,51,49,32,52,46,52,57,52,49,52,49,32,48,46,48,48,48,48,48,48,10,118,32,32,53,46,57,52,53,51,49,51,32,52,46,54,49,55,49,56,56,32,48,46,48,48,48,48, 
    //~ 48,48,10,118,32,32,54,46,49,55,53,55,56,49,32,52,46,52,57,52,49,52,49,32,48,46,48,48,48,48,48,48,10,118,32,32,54,46,52,50,57,54,56,56,32,52,46,49,50,53,48,48,48,32,48,46,48,48,48,48,48,48,10,118,32,32,53,46,51,56,55,49,56,56,32,52,46,49,50,53,48,48,48 
    //~ }; 
    // move definition to .cpp because of 'error: in-class initialization of static data member ‘const unsigned char MyStuff::temp_binary_data_7 []’ of incomplete type' 
    static const unsigned char temp_binary_data_7[]; 

    const char* teapot_obj = (const char*) temp_binary_data_7; 

    // from OpenGLDemo.cpp: 
    struct Shape 
    { 
    Shape() 
    { 
     std::cout << "initializing " << static_cast<void*>(teapot_obj) << std::endl ; 
    } 
    }; 
}; 

mystuff.cpp

#include "mystuff.h" 

const unsigned char MyStuff::temp_binary_data_7[] = 
{ 35,32,77,97,120,50,79,98,106,32,86,101,114,115,105,111,110,32,52,46,48,32,77,97,114,32,49,48,116,104,44,32,50,48,48,49,10,35,10,35,32,111,98,106,101,99,116,32,84,101,97,112,111,116,48,49,32,116,111,32,99,111,109,101,32,46,46,46,10,35,10,118,32,32,53, 
46,57,50,57,54,56,56,32,52,46,49,50,53,48,48,48,32,48,46,48,48,48,48,48,48,10,118,32,32,53,46,56,51,50,48,51,49,32,52,46,52,57,52,49,52,49,32,48,46,48,48,48,48,48,48,10,118,32,32,53,46,57,52,53,51,49,51,32,52,46,54,49,55,49,56,56,32,48,46,48,48,48,48, 
48,48,10,118,32,32,54,46,49,55,53,55,56,49,32,52,46,52,57,52,49,52,49,32,48,46,48,48,48,48,48,48,10,118,32,32,54,46,52,50,57,54,56,56,32,52,46,49,50,53,48,48,48,32,48,46,48,48,48,48,48,48,10,118,32,32,53,46,51,56,55,49,56,56,32,52,46,49,50,53,48,48,48 
}; 

MyStuff::MyStuff() { 

} 
MyStuff::~MyStuff() { 

} 

當我g++編譯,我得到這個:

$ g++ -std=c++11 main.cpp mystuff.cpp -o main 
In file included from main.cpp:3:0: 
mystuff.h: In constructor ‘MyStuff::Shape::Shape()’: 
mystuff.h:18:42: error: invalid use of non-static data member ‘MyStuff::teapot_obj’ 
    const char* teapot_obj = (const char*) temp_binary_data_7; 
             ^
mystuff.h:25:58: error: from this location 
     std::cout << "initializing " << static_cast<void*>(teapot_obj) << std::endl ; 
                 ^
In file included from mystuff.cpp:1:0: 
mystuff.h: In constructor ‘MyStuff::Shape::Shape()’: 
mystuff.h:18:42: error: invalid use of non-static data member ‘MyStuff::teapot_obj’ 
    const char* teapot_obj = (const char*) temp_binary_data_7; 
             ^
mystuff.h:25:58: error: from this location 
     std::cout << "initializing " << static_cast<void*>(teapot_obj) << std::endl ; 
                 ^

這只是發生時mystuff.h存在struct Shape碼 - 如果你刪除它,然後將代碼編譯並運行正常。

那麼我有什麼選擇?我怎樣才能定義struct Shape(或其他變量),以便它可以引用teapot_obj沒有編譯錯誤?

回答

0

好,管理由剛剛在這裏和那裏投擲表達式來修復它,但它仍然將是巨大的閱讀,解釋什麼是真正回事答案...這裏是我的改變 - 只有在mystuff.h

mystuff.h

#include <iostream> 

class MyStuff 
{ 
public: 
    MyStuff(); 
    ~MyStuff(); 

    static const unsigned char temp_binary_data_7[]; 
    static constexpr const char* teapot_obj = (const char*) temp_binary_data_7; 

    // from OpenGLDemo.cpp: 
    struct Shape 
    { 
    Shape() 
    { 
     std::cout << "initializing " << static_cast<const void*>(teapot_obj) << std::endl ; 
    } 
    }; 
    Shape tmptest; 
}; 

因此,基本上:

const char* teapot_obj = (const char*) temp_binary_data_7; 

不得不改變成:

static constexpr const char* teapot_obj = (const char*) temp_binary_data_7; 

......這意味着我必須製作一個static_cast<const void*>(而不是static_cast<void*>)來打印出對象地址;最後,必須添加一個Shape tmptest;以便Shape的構造函數至少運行一次,這樣我們就可以打印一些東西。

而現在,程序運行沒有問題:

$ g++ -std=c++11 main.cpp mystuff.cpp -o main 
$ ./main 
initializing 0x400c60 
hello world# 
相關問題