如何在iphone项目中添加C ++文件

编程入门 行业动态 更新时间:2024-10-22 20:22:55
本文介绍了如何在iphone项目中添加C ++文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个iPhone项目,在这里我想使用c ++文件。我创建了如下所示的c ++文件:

File-> New File - > C / C ++ files - > C ++ File 它为 ClassA.cpp

在ClassA.cpp 中

#include< iostream> class ClassA { public: int a,b; void add(); }; void ClassA :: add() { // printf(sdf); $ file

#importViewController.h #importClassA.cpp - (void)viewDidLoad { ClassA a; a.add(); [super viewDidLoad]; //在加载视图之后执行任何其他设置,通常来自nib。 }

我读了一些帖子,我说我把.m重命名为.mm,所以我将所有.m文件重命名为.mm 并添加了两个链接器标志

-cclib -lstdc ++

但它给出以下错误:

解决方案

您需要将C ++部分拆分为头文件和 cpp 文件,否则 ClassA :: add 被定义两次。

ClassA.h:

#include< ; iostream> class ClassA { public: int a,b; void add(); };

ClassA.cpp:

#includeClassA.h void ClassA :: add() { // printf(sdf); }

您的.mm文件:

#importClassA.h ...文件的其余部分...

I have an iPhone project, in this I wanted to use c++ files. I created c++ file like below:

File->New File -> C/C++ files -> C++ File and named it as ClassA.cpp

In ClassA.cpp

#include <iostream> class ClassA { public: int a, b; void add(); }; void ClassA::add() { // printf("sdf"); }

in my viewController.mm file:

#import "ViewController.h" #import "ClassA.cpp" - (void)viewDidLoad { ClassA a; a.add(); [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. }

I read some posts it says me to rename .m to .mm so I renamed all .m files to .mm And added two linker flags

-cclib -lstdc++

But It gives the following error:

解决方案

You need to split the C++ portion into a header file and a cpp file, otherwise the ClassA::add will be defined twice.

ClassA.h:

#include <iostream> class ClassA { public: int a, b; void add(); };

ClassA.cpp:

#include "ClassA.h" void ClassA::add() { // printf("sdf"); }

Your .mm file:

#import "ClassA.h" ... the rest of the file ...

更多推荐

如何在iphone项目中添加C ++文件

本文发布于:2023-07-16 20:17:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1125486.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   项目   如何在   iphone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!