Clear Filters
Clear Filters

C++ compilation error

4 views (last 30 days)
Maher
Maher on 31 Oct 2018
Commented: Walter Roberson on 21 Dec 2018
Dear all,
Why am I getting this error when I compile using mex. when I compile using Xcode 'clang++' everything is fine.
******************************
#include <iostream>
#include "mex.hpp"
#include "mexAdapter.hpp"
using namespace std;
using namespace matlab::data;
using matlab::mex::ArgumentList;
class Z {
public:
void get (){
cin>> x;
}
void print (){
cout<<"the integer:" << x;
}
private:
int x;
};
class MexFunction : public matlab::mex::Function {
Z abd;
abd.get();
abd.print();
};
***************************************error*************>>
mex -setup C++
MEX configured to use 'Xcode Clang++' for C++ language compilation.
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. You will be required
to update your code to utilize the new API.
You can find more information about this at:
https://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
>> mex maher1.cpp
Building with 'Xcode Clang++'.
Error using mex
/Users/maherheal/Dropbox/maherphd/1UK/Last/Code_v3/maher1.cpp:22:5: error: unknown type name 'abd'
abd.get();
^
/Users/maherheal/Dropbox/maherphd/1UK/Last/Code_v3/maher1.cpp:22:8: error: expected member name or ';'
after declaration specifiers
abd.get();
~~~^
/Users/maherheal/Dropbox/maherphd/1UK/Last/Code_v3/maher1.cpp:23:5: error: unknown type name 'abd'
abd.print();
^
/Users/maherheal/Dropbox/maherphd/1UK/Last/Code_v3/maher1.cpp:23:8: error: expected member name or ';'
after declaration specifiers
abd.print();
~~~^
4 errors generated.

Accepted Answer

Viggnesh Venkatakrishnan
Viggnesh Venkatakrishnan on 17 Dec 2018
Hi Maher,
I understand that you are having a problem with compiling the given code using MEX. I tried reproducing the issue at my end and observed the following:
class MexFunction : public matlab::mex::Function {
Z abd;
abd.get();
abd.print();
};
In the above part of the code MexFuntion is a class which has an object of the class Z as one of the data members. The next line of the code calls the member funtion of class Z using the same object that was created. C++ does not allow a funtion/Method call directly inside a class. You can do it in the following way instead:
class MexFunction : public matlab::mex::Function {
Z abd;
MexFuntion()
{
abd.get();
abd.print();
}
};
In the above code snippet, the Method of class Z is being called after the Data member abd has been constructed.
If you can any specific questions regarding c++ mex please refer to the example given in the doc :https://www.mathworks.com/help/matlab/matlab_external/c-class-in-mex-files.html
Thank you ,
Viggnesh Venkatakrishnan
  2 Comments
Maher
Maher on 21 Dec 2018
Thank you very much!
Walter Roberson
Walter Roberson on 21 Dec 2018
Is MexFuntion() a typing mistake ? Should it be MexFunction() ?

Sign in to comment.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!