C++到 Objective-C ,在 for 循环中使用 const int

发布于 2024-12-14 10:04:41 字数 293 浏览 0 评论 0原文

在我的头文件啊,我

const int a = 4;

的源文件 a.cpp (扩展名是 cpp,但类型是 Objective c ),我有

#import "a.h"

int func(){
    for(int i = 0; i < a -1 ; i++){
    }
}

但是,在编译这个时,我收到错误“找不到'a'的协议声明” ”。

为什么会发生这种情况以及如何解决这个问题?

谢谢 !

In my header file a.h, I have

const int a = 4;

in my source file a.cpp ( the extension is cpp, but the type is objective c ), i have

#import "a.h"

int func(){
    for(int i = 0; i < a -1 ; i++){
    }
}

However, on compiling this, I get the error "cannot find protocol declaration for 'a'".

Why is this occurring and how can I fix this ?

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

美羊羊 2024-12-21 10:04:41

就其本身而言,您的代码是正确的 - 不需要协议,并且如果您这样做,编译定义为 sourcecode.cpp.objcppa.cpp 将会被阻塞。

附带说明一下,您可以将 ah 定义为 sourcecode.ch,即使它包含纯 C++ - 这就是 #includes 文件的方式> 或 #imports 属于重要的类别。

我见过一些递归包含/导入可能会生成这样的虚假错误的实例。

除此之外,XCode 项目可能已被破坏——您是否尝试过设置一个新的骨架项目并向其中添加这些文件?

In itself, your code is correct -- there is no need for a protocol and compiling a.cpp defined as a sourcecode.cpp.objcpp would choke if you did.

As a side note, you can leave a.h defined as a sourcecode.c.h even if it contains pure C++ -- it's how the file that #includes or #imports is categorized that matters.

I've seen some instances where recursive includes/imports can generate spurious errors like this.

Other than that, It's possible that the XCode project has gotten munged -- have you tried setting up a new skeleton project and adding these files to it?

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