0

Schleife.h

#ifndef __CPlusPlusTest__Schleife__
#define __CPlusPlusTest__Schleife__

#include <iostream>
class Schleife;
#endif /* defined(__CPlusPlusTest__Schleife__) */

Schleife.cpp

#include "Schleife.h"

class Schleife
{
public:
    int addition(int,int);

private:
    int ergebnis;
};


int Schleife::addition(int a,int b)
{
    ergebnis = a +b;
    return ergebnis;
}

ViewController.h

#import <UIKit/UIKit.h>

#import "Schleife.h"

@interface ViewController : UIViewController

@end

ViewController.mm

#import "ViewController.h"



@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
     Schleife *schleife = new Schleife();
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Whats Wrong?

I got the Error Message: "ViewController.mm:22:31: Allocation of incomplete type 'Schleife'"

I don't understand what I did wrong. I want just implement C++ Code in my App. So I started with a easy example but it doesn't work... Can you help me? I know its maybe a easy Questions but I can't find the mistake...

2
  • 1
    i'm not sure but maybe you should put Schleife class declaration into Schleife.h? Commented Apr 2, 2014 at 6:52
  • I'm actually sure that without seeing the declaration of that class, neither Objective-C++ nor plain old C++ would be able to compile the new () call nor access any methods of the C++ class. This is nothing particular to Objective-C. That's what header files are for. Commented Apr 2, 2014 at 7:01

1 Answer 1

2

Schleife.h should have this code instead of the cpp file

class Schleife
{
public:
    int addition(int,int);

private:
    int ergebnis;
};

you will also need to include #include "Schleife.h" in your ViewController.mm

Sign up to request clarification or add additional context in comments.

6 Comments

but How i can use this return in my ObjC Code? int ergebnis = schleife::addition(10, 20); NSLog(@"%d",Ergebnis); i got this error Message: ViewController.mm:23:20: Use of undeclared identifier 'schleife'; did you mean 'Schleife'? and ViewController.mm:23:30: Call to non-static member function without an object argument
You can safely use this in your mm file, there should be no issues. What you need to do is use the mm file as a bridge between the cpp and the m world.
like a bridge? but How i get the cpp Types convert to ObjC Types
Primitive types can convert. the Classes do not convert at all. let me try to elaborate further in my answer
Looking at your code, you are missing #include "Schleife.h" in your mm file
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.