Not sure about Expected a type

Asked 2 years ago, Updated 2 years ago, 51 views

■ C++ Class Header → Test.h

#ifndef TEST_H
#define TEST_H
# include "TestDef.h"
void Test(TEST_STR&test_str);
#endif//TEST_H

■ C++ Define Header → TestDef.h

#ifndef TESTDEF_H
#define TESTDEF_H
struct TEST_STR{
    inta;
    intb;
};
#endif

■C++ Class → Test.cpp is now Test.mm.

#include "Test.h"
# include "TestDef.h"

void Test(TEST_STR&test_str)
{
}

■Obje-c++ Header→TestObj.h

#ifndefTestObj_h
#define TestObj_h

#import<Foundation/Foundation.h>
# import "TestDef.h"
@interfaceTestObj:NSObject    
- (void) test: (TEST_STR&) test;
@end

#endif /*TestObj_h*/

■Obj-C++ Source→TestObj.mm

#import<Foundation/Foundation.h>
# import "TestObj.h"
# import "Test.h"

@implementation TestObj

- (void) test: (TEST_STR&)a{
    Test(a);
}

@end

■ I put #import "TestObj.h" in the bridge file.

It looks like this, but
■Expected a type appears in the Obje-c++ header below.

-(void)test:(TEST_STR&)test;

When I searched for the above error, it said that there was an additional #import, so I added that as well.
As always...
I was able to jump from XCODE to TEST_STR, so I was able to refer to it
I think, but I have no idea what's wrong.
I would appreciate it if you could advise me.

objective-c

2022-09-29 22:17

1 Answers

(As you write, it is important how each file is imported in a state where you need to add #import, so it is better to include not only an overview of the files like the C++ class header but also the names of the files.> Thank you for the postscript.The corrections related to the postscript are at the bottom.)

If you put the code in a file with the following file name, the compilation will pass.

  • C++ Class Header - > Test.h
  • C++ Define Header ->TestDef.h
  • C++ class->Test.cpp
  • Obje-c++ Header ->TestObj.h
  • Obj-C++ Source - >TestObj.mm

It says C++ class (change to mm extension), but if you don't set the .mm extension for Obj-C++ source, you won't be able to use the C++ data type within TestObj.h imported from it.Please re-check the contents of each file, including the name and import.

[Additional note]
If you compare the additional information in your question with my above content,

  • C++ class->Test.cpp is now Test.mm

There is a difference in that, but this is not a problem.Since only C++ features are used in the C++ class, the results will be the same whether you compile it as "C++" or "Objective-C++."

It seems that this is the one that affects you.

  • I put "#import"TestObj.h" in the bridge file

Did you add a C++ or Objective-C++ file to the Swift project when you created the project in Xcode?If you reference Bridging-Header during the Swift project build process, it is interpreted as a header for Objective-C (not Objective-C++).Therefore, the reference type of C++ will not be recognized as a type, so it will be "Expected a type" at that time.

If you create the wrapper class in Objective-C++ to take advantage of the functionality of the function written in C++ from Swift, you should not use the functionality of C++ in the @interface portion of the class you want Swift to read.
 For example, the only way to do this is to use a pointer (not a reference type).

■Obj-c++ Header → TestObj.h

#ifndefTestObj_h
#define TestObj_h

#import<Foundation/Foundation.h>
# import "TestDef.h"
@interfaceTestObj:NSObject
- (void) test: (TEST_STR*) test;
@end

#endif /*TestObj_h*/

■Obj-C++ Source→TestObj.mm

#import<Foundation/Foundation.h>
# import "TestObj.h"
# import "Test.h"

@implementation TestObj

- (void) test: (TEST_STR*)a{
    Test(*a);
}

@end

It seems that the error is still coming out as above.The definition of the TEST_STR type must be written in such a way that it can pass as "C++" or "C".

■ C++ Define Header → TestDef.h

#ifndef TESTDEF_H
#define TESTDEF_H
typeef structure TEST_STR{
    inta;
    intb;
} TEST_STR;
#endif

What do you think?Please let me know if there is anything.


2022-09-29 22:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.