我试图将值从Level类传递给Grid类,并且在grid_w和grid_h中发生访问冲突。
我尝试过使用extern,但它说invalid storage class for a class member
。我尝试使用Grid a; a.setgrid(5);
但它无法传递该值。
有人可以向我解释一下吗?
Level.cpp
#include "Level.h"
#include "Grid.h"
USING_NS_CC;
using namespace CocosDenshion;
Grid* layer25;
CCScene* Level::scene()
{
CCScene *scene = CCScene::create();
Level *layer = Level::create();
layer25 = Grid::create();
layer25->retain();
scene->addChild(layer);
scene->addChild(layer25);
return scene;
}
// on "init" you need to initialize your instance
bool Level::init()
{
if ( !CCLayer::init() )
{
return false;
}
else {
setTouchEnabled(true);
}
Grid pass;
pass.setgrid(5); // send value here, retreive in Grid.h
return true;
}
Level.h
#ifndef __LEVEL_H__
#define __LEVEL_H__
#include "cocos2d.h"
using namespace cocos2d;
class Grid;
class Level : public cocos2d::CCLayer
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
CREATE_FUNC(Level);
};
#endif // __HELLOWORLD_SCENE_H__
Grid.cpp
#include "Grid.h"
using namespace CocosDenshion;
USING_NS_CC;
CCLayer* Grid::scene()
{
CCScene *scene = CCScene::create();
OnetGrid *layer2 = OnetGrid::create();
scene->addChild(layer2);
return layer2;
}
// on "init" you need to initialize your instance
bool Grid::init()
{
if ( !CCLayer::init() )
{
return false;
}
else {
setTouchEnabled(true);
}
grid();
return true;
}
void Grid::grid()
{
int a = 0;
int b = 0;
cakes = CCArray::createWithCapacity(grid_w*grid_h);
cakes->retain();
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
if (grid_w == 10 && grid_h == 12) {
int x = 51.5 * grid_w/2;
int y = 55 * grid_h/2;
int random[120];
int rnd;
int rnd2;
srand (time(NULL));
for (int l = 0; l < 60; l++) {
rnd = rand()%20 + 1;
rnd2 = rnd;
random[l] = rnd;
random[l+60] = rnd2;
}
int l = 0;
for (int j = 0 ; j < grid_h; j++) {
for ( int k = 0; k < grid_w; k++) {
CCLabelTTF* tests = CCLabelTTF::create("Done", "Life is goofy", 60);
tests->setColor(ccc3(0, 0, 0));
tests->setPosition(ccp(400, 400));
this->addChild(tests, 5);
}
}
} else {
// but being executed in here
char text[256];
sprintf (text, "%d, %d", grid_h, grid_w);
CCLabelTTF* tests_ = CCLabelTTF::create(text, "Life is goofy", 60);
test_->setColor(ccc3(0, 0, 0));
test_->setPosition(ccp(400, 400));
this->addChild(test_, 5);
}
}
Grid.h
#ifndef _GRID_H_
#define _GRID_H_
#include "cocos2d.h"
USING_NS_CC;
using namespace cocos2d;
class Grid : public CCLayer
{
public:
int grid_w;
int grid_h;
virtual bool init();
void setgrid(int a) //retreive in here, print in Grid.cpp
{
switch (a){
case 1:
grid_w = 6;
grid_h = 6;
break;
case 2:
grid_w = 6;
grid_h = 9;
break;
case 3:
grid_w = 8;
grid_h = 9;
break;
case 4:
grid_w = 9;
grid_h = 10;
break;
case 5:
grid_w = 10;
grid_h = 12;
break;
default:
grid_w = 10;
grid_h = 12;
break;
}
}
int getgrid_w() {
return grid_w;
}
int getgrid_h() {
return grid_h;
}
static cocos2d::CCLayer* scene();
void grid();
CREATE_FUNC(Grid);
};
#endif
答案 0 :(得分:1)
您已向对象声明了一个名为pass
的指针,但尚未为其分配任何内存。指针pass
可以包含任何随机值,可能是也可能不是有效地址。
从未分配位置访问内存为Undefined behavior
。
所以你需要使用new
分配内存,如下所示:
Grid* pass = new Grid;
这为免费存储中的对象分配内存,以便您可以访问数据成员。一旦完成,请记得释放记忆。
您还可以将对象存储在连续的内存中,如下所示:
Grid gr_obj;
Grid* pass = &gr_obj;
答案 1 :(得分:0)
如果不为指针pass
分配内存,它如何工作?
答案 2 :(得分:0)
您尚未初始化pass
。因此,使用未初始化的指针调用方法是未定义的行为。
您需要以下内容:
pass = new Grid();
答案 3 :(得分:0)
我找到了答案。 Level *layer = Level::create();
首先调用Level::init()
,因此在执行Grid pass; pass.setgrid(5);
时,它返回Undefined behavior
,因为网格层没有分配任何内存。
我所做的是在创建网格图层后移动Level *layer = Level::create();
:
CCScene* Level::scene()
{
CCScene *scene = CCScene::create();
layer25 = Grid::create();
layer25->retain();
Level *layer = Level::create();
scene->addChild(layer);
scene->addChild(layer25);
return scene;
}