I am pretty much new to cocos2dx and c++ as well. I am having problems in passing data between classes.
Here is My problem
I have a class1, in which i need to set value of a variable and call a function of an another class let's say "Class2".
I need to do this in class1 without making an object of class2.
What i did so far is like the following.
Class1.cpp
#include "class2.h"
void Class1::methodinClassOne()
{
class2::imageName = this->str;
class2::doSth();
}
class2.h
class class2 {
public:
std::string imageName;
static void doSth();
};
class2.mm
#include "class2.h"
using namespace cocos2d;
void class2::doSth() {
id sth = [[UIApplication sharedApplication] delegate];
if ([sth isKindOfClass:[AppController class]])
{
printf("class2::doSth imageName %s",imageName.c_str());
SpriteVC *SPVC = [[SpriteVC alloc] initWithNibName:nil bundle:nil];
SPVC.imageNameString = [NSString stringWithFormat:@"%s",imageName.c_str()];
NSLog(@"class2::doSth imageName == %@",[NSString stringWithFormat:@"%s",imageName.c_str()]);
SPVC.imageView.frame = CGRectMake(480, 320, 333, 333);
AppController *controller = (AppController *)sth;
[controller.viewController.view addSubview:SPVC.imageView];
}
}
The Error Is like the Following
inside class2.h I also tried using
static std::string imageName;
instead of
std::string imageName;
But then it gives me the following error
Undefined symbols for architecture i386:"class2::imageName", referenced from:class2::doSth() in XBridge.o
I know i am missing a very basic concept of C++ here. but couldn't seems to find whats wrong. Please help me out here. thanks
In addition to their declaration, static member variables have to be defined outside of the class body. So first, you have to declare your member variable imageName
as static in the class2
header file, and then you also have to define the variable in the corresponding mm file:
class2.h
class class2 {
public:
static std::string imageName;
static void doSth();
};
class2.mm
#include "class2.h"
std::string class2::imageName;
//other stuff
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.