变量不是类的静态成员

发布于 2024-11-29 07:07:33 字数 832 浏览 2 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海风掠过北极光 2024-12-06 07:07:33

这是一个海湾合作委员会错误。请参阅 Objective-C++ 块与 Objective-C 块 了解其中之一对此的报道。

我建议尽可能避免使用 Objective-C++。编译速度慢,运行臃肿(尤其是 ARC,因为它打开 -fobjc-arc-exceptions ),编译器和调试器中存在错误,而且在我的经验中,大部分都是一团糟,给了两个世界最糟糕的结果。 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.

美人迟暮 2024-12-06 07:07:33

如果它可能对其他人有帮助...

我的项目需要需要使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文