简体   繁体   English

变量不是 class 的 static 成员

[英]Variable is not a static member of class

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.我正在研究 GameKitHelper class,它主要是用 C++ 编写的,但在某些地方也使用 Objective-C,在 .mm 文件中。

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'

This is a gcc bug.这是一个 gcc 错误。 See Objective-C++ block vs Objective-C block for one of many reports of it.有关它的众多报告之一,请参阅Objective-C++ 块与 Objective-C 块

<soapbox>I recommend avoiding Objective-C++ as much as possible. <soapbox>我建议尽可能避免使用 Objective-C++。 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.编译速度慢,运行臃肿(尤其是使用 ARC,因为它打开了-fobjc-arc-exceptions ),编译器和调试器中的错误,而且在我的经验中大部分都是一团糟,给出了两全其美的结果。 C++ is fine. C++ 没问题。 Objective-C is fine. Objective-C 没问题。 Just keep the interface between them as small as possible.只要保持它们之间的接口尽可能小。 </soapbox> </肥皂盒>

But switching to clang 2.0 might fix this specific problem.但是切换到 clang 2.0 可能会解决这个特定问题。

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.我的项目需要我使用 10.6 SDK 和 LLVM-gcc 4.2。 I cannot require clang for the code to compile.我不能要求 clang 来编译代码。

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.我通过在父 function 中将我的变量声明为 shared_ptr 来解决这个问题...将我需要的实际 object 放在堆上。 In the block, I access the object through the shared_ptr variable.在该块中,我通过 shared_ptr 变量访问 object。 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.这种安排导致 shared_ptr 被隐式复制到块中,而父 function 中的副本可以自由释放。 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.如果代码是在 clang 或其他编译器上构建的,我使用预处理器检查来使用普通堆栈变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM