无效使用非静态数据成员来访问结构中的数组

时间:2017-04-03 08:23:57

标签: c++

我正在尝试使用JUCE演示,并将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 ;
                                                          ^

仅当struct Shape中存在mystuff.h代码时才会发生这种情况 - 如果删除它,则代码编译并运行正常。

那么我的选择是什么?如何定义struct Shape(或其他变量),以便在没有编译错误的情况下引用teapot_obj

1 个答案:

答案 0 :(得分: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#