变量不是类的静态成员
我正在开发一个 GameKitHelper 类,它主要是用 C++ 编写的,但在 .mm 文件内的某些地方也使用 Objective-C 编写。
我删除了一些功能来隔离错误:
void GameKitHelper::PopulateFriendScores(DynArray<GameCenterScore> *FriendScores)
{
GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
if (leaderboardRequest != nil)
{
leaderboardRequest.playerScope = GKLeaderboardPlayerScopeFriendsOnly;
leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime;
leaderboardRequest.range = NSMakeRange(1,25);
[leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error)
{
int i = 0;
printf("%d", i);
}];
}
}
我在这里得到的错误是:
'int GameKitHelper::i' is not a static member of 'class GameKitHelper'
I'm working on a GameKitHelper class, and it's mostly written in C++, but with Objective-C in some places as well, inside an .mm file.
I removed a bit of functionality to isolate the error:
void GameKitHelper::PopulateFriendScores(DynArray<GameCenterScore> *FriendScores)
{
GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
if (leaderboardRequest != nil)
{
leaderboardRequest.playerScope = GKLeaderboardPlayerScopeFriendsOnly;
leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime;
leaderboardRequest.range = NSMakeRange(1,25);
[leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error)
{
int i = 0;
printf("%d", i);
}];
}
}
The error I get here is:
'int GameKitHelper::i' is not a static member of 'class GameKitHelper'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个海湾合作委员会错误。请参阅 Objective-C++ 块与 Objective-C 块 了解其中之一对此的报道。
但切换到 clang 2.0 可能会解决这个特定问题。
This is a gcc bug. See Objective-C++ block vs Objective-C block for one of many reports of it.
<soapbox>I recommend avoiding Objective-C++ as much as possible. It's slow to compile, bloated to run (particularly with ARC since it turns on -fobjc-arc-exceptions), buggy in the compiler and the debugger, and mostly a mess in my experience giving the worst of both worlds. C++ is fine. Objective-C is fine. Just keep the interface between them as small as possible. </soapbox>
But switching to clang 2.0 might fix this specific problem.
如果它可能对其他人有帮助...
我的项目需要需要使用 10.6 SDK 和 LLVM-gcc 4.2。我不能要求 clang 来编译代码。
我通过在父函数中将变量声明为shared_ptr来解决这个问题...将我需要的实际对象放在堆上。在块中,我通过shared_ptr变量访问该对象。这种安排导致shared_ptr被隐式复制到块中,而父函数中的副本可以自由释放。由于我没有在块中声明变量,因此我绕过了该错误。
如果代码是在 clang 或其他编译器上构建的,我使用预处理器检查来使用正常的堆栈变量。
In case it might help someone else...
I am required by the needs of my project to use the 10.6 SDK and LLVM-gcc 4.2. I cannot require clang for the code to compile.
I worked around this problem by declaring my variable as a shared_ptr in the parent function... putting the actual object I need on the heap. In the block, I access the object through the shared_ptr variable. This arrangement causes the shared_ptr to be implicitly copied into the block while the copy in the parent function is free to be released. Since I am not declaring a variable in the block, I bypass the bug.
I used a preprocessor check to use a normal stack variable if the code is building on clang or some other compiler.