我正在嘗試使用JUCE Demo,並將BinaryData.cpp和OpenGLDemo.cpp的部分抽取到我自己的類中。不幸的是,我遇到一個問題我真的不能解析,我已經成功的三個文件這個小例子,以減少:main.cpp
,mystuff.cpp
和mystuff.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
沒有編譯錯誤?