I am trying to mix Swift and C++, and my C++ class has errors "expected ';' after top level declarator" and "unknown type name 'class'; did you mean 'Class'?"

I am attempting to create a C++ file called Fish and to display the fish's num in my SwiftUI app. However, I am encountering the errors "expected ';' after top level declarator" and "unknown type name 'class'; did you mean 'Class'?". It appears that Xcode thinks that the file is a C file. How should I address these errors?

Replies

Did you set the C++ and Objective-C Interoperability build setting to C++ / Objective-C++ (objcxx)? I created a tiny test project to exercise this tech and I got exactly this error because that build setting defaults to C / Objective-C (objc).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

  • I did set it to C++ / Objective-C++. I was able to solve this bug by inserting #ifdef __cplusplus to before class Fish and #endif after };, but I don't think that is ideal.

  • I did set the C++ and Objective-C Interoperability build setting to C++ / Objective-C++. I was able to fix it by adding #ifdef __cplusplus and #endif around my code, but that isn't an ideal solution, so any further pointers on how to solve this would be greatly appreciated.

Add a Comment

Set "Supported Languages" to exactly objective-c++ in "Build Settings", then everything works as expected. @jinyangz