0
我對於cocos2dx和C++來說也是很新的東西。我在類之間傳遞數據時遇到了問題。如何從類的外部設置成員變量並在C++中使用類內的變量?
這裏是我的問題
我有一個class1的,在我需要設置一個變量的值,並調用另一個類的功能,讓我們說「等級2」。
我需要在class1中做到這一點,而無需製作class2的對象。
到目前爲止,我做了如下的事情。
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];
}
}
錯誤是像下面
內class2.h 我也嘗試過使用
static std::string imageName;
,而不是
std::string imageName;
但後來它給了我下面的錯誤
Undefined symbols for architecture i386:"class2::imageName", referenced from:class2::doSth() in XBridge.o
我知道我在這裏錯過了一個非常基本的C++概念。但似乎無法找到什麼是錯的。 請幫我看看這裏。 謝謝
感謝這解決了我的問題 –