Reputation: 2778
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:
Upvotes: 1
Views: 1380
Reputation: 726479
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 ...
Upvotes: 3