为什么在调用[myclass alloc] misx c ++到object-c ++到object-c时出错?

如何解决为什么在调用[myclass alloc] misx c ++到object-c ++到object-c时出错?

我尝试从对象c使用调用c ++
使用B4I 此代码,但我得到错误 我已经在使用file.mm

文件Greeting.cpp

#include "Greeting.hpp"

Greeting::Greeting() {
    greeting = "Hello C++!";
}

std::string Greeting::greet() {
    return greeting;
}

文件Greeting.hpp


    #ifndef Greeting_hpp
#define Greeting_hpp

#include <stdio.h>
#include <string>

class Greeting {
    std::string greeting;
public:
    Greeting();
    std::string greet();
};
#endif /* Greeting_hpp */

文件ViewController.h


    #import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (void)showGreeting;

@end

文件ViewController.mm


#import "ViewController.h"
#import "Greeting.hpp"

@interface ViewController ()
{
    Greeting greeting;
    IBOutlet UIButton *helloButton;
}
@end

@implementation ViewController

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

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

- (void)showGreeting {

 NSLog(@"test %@");

}


@end

我尝试称呼它


 ViewController* ViewController1= [[ViewController alloc] init];
  [ ViewController1 showGreeting ];

我收到错误未知方法

-------------------------------
Undefined symbols for architecture arm64:
  "_OBJC_CLASS_$_ViewController",referenced from:
      objc-class-ref in b4i_main.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command Failed with exit code 1 (use -v to see invocation)
---------------------------------

当我使用

ViewController* ViewController1;
  [ ViewController1 showGreeting ];

返回null 注意此示例HelloCpp-master

谢谢

解决方法

以下示例显示了如何使用AppDelegate中的方法从objc应用程序调用c ++类。它可以在终端中运行,方法是先将源代码保存在名为“ greet.mm”的文件中,然后使用以下命令:clang greet.mm -framework Cocoa -o greet && ./greet

//clang greet.mm -framework Cocoa -o greet && ./greet

#import <Cocoa/Cocoa.h>

class Greeting {
public:
  void greet();
};

void Greeting::greet() {
  NSLog(@"Hello C++!");
}

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
  NSWindow *window;
}
- (void) myBtnAction: (id)sender;
- (void) buildMenu;
- (void) buildWnd;

@end

@implementation AppDelegate

- (void) myBtnAction: (id)sender {
  Greeting g;
  g.greet();
}

- (void) buildMenu {
 NSMenu *menubar = [NSMenu new];
 NSMenuItem *menuBarItem = [NSMenuItem new];
 [menubar addItem:menuBarItem];
 [NSApp setMainMenu:menubar];
 NSMenu *appMenu = [NSMenu new];
 NSMenuItem *quitMenuItem = [[NSMenuItem alloc] initWithTitle:@"Quit"
 action:@selector(terminate:) keyEquivalent:@"q"];
 [appMenu addItem:quitMenuItem];
 [menuBarItem setSubmenu:appMenu];
}

- (void) buildWnd {    
#define _wndW  200
#define _wndH  150
 window = [ [ NSWindow alloc ] initWithContentRect:NSMakeRect( 0,_wndW,_wndH )
    styleMask: NSWindowStyleMaskClosable | NSWindowStyleMaskTitled
    backing:NSBackingStoreBuffered defer:YES];
 [window center];
 [window setTitle: @"Test window" ];
 [window makeKeyAndOrderFront:nil];
    
// **** My Button **** //
 NSButton *myBtn =[[NSButton alloc]initWithFrame:NSMakeRect( 40,80,125,24 )];
 [myBtn setBezelStyle:NSBezelStyleRounded ];
 [myBtn setTitle: @"Run CPP Class"];
 [myBtn setAction: @selector (myBtnAction:)];
 [[window contentView] addSubview: myBtn];

// **** Quit btn **** //
 NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 50,10,40,40 )];
 [quitBtn setBezelStyle:NSBezelStyleCircular ];
 [quitBtn setTitle: @"Q" ];
 [quitBtn setAction:@selector(terminate:)];
 [[window contentView] addSubview: quitBtn];
}

- (void) applicationWillFinishLaunching: (NSNotification *)notification {
[self buildMenu];
[self buildWnd];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  // Insert code here to initialize your application
}

@end

int main () {
 NSApplication *app = [NSApplication sharedApplication];
 AppDelegate *appDelegate = [[AppDelegate alloc] init];
 [app setDelegate:appDelegate];
 [NSApp run];
 return 0;
}


,

应在iOS中运行。将ViewController的扩展名从.m更改为.mm

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@end

#import "ViewController.h"

class Greeting {
public:
  void greet();
};

void Greeting::greet() {
  NSLog(@"Hello C++!");
}

@interface ViewController ()
@property (strong,nonatomic) IBOutlet UIButton *LaunchBtn;

@end

@implementation ViewController
- (IBAction)showGreeting:(id)sender {
  Greeting g;
  g.greet();
}

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
}

@end

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?