Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6042541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:48:08+00:00 2026-05-23T06:48:08+00:00

I am trying to create a simple C++ class and a Matlab mex file.

  • 0

I am trying to create a simple C++ class and a Matlab mex file. My code is as follows:

Matlab: mexTest1.cpp

#include "mex.h"
#include "K:\\My Documents\\Visual Studio 2010\\Projects\\HelloWorld\\HelloWorld\\Class1.h"

/* Input Arguments */
#define X   prhs[0]

/* Output Arguments */
#define RESULT  plhs[0]

void mexFunction( int nlhs, mxArray *plhs[], 
          int nrhs, const mxArray *prhs[] ){ 
    /* Initialize input and output pointers
    // Inputs */
    double *x; 
    /* Outputs */
    double r;
    double *result; 

    mwSize m,n; 

    m = mxGetM(X); 
    n = mxGetN(X); 

    /* Create a matrix for the return argument */ 
    RESULT = mxCreateDoubleMatrix(1, 1, mxREAL);
    /* Assign pointers to the various parameters */ 
    result = mxGetPr(RESULT);

    x = mxGetPr(X);

    /* Do the actual computations in a subroutine */
    Class1 c1(2, 15.0);
    r = c1.product();

    result[0] = r;
    return;

}

Class1.h:

#pragma once

#include <string> // Standard string class in C++

class Class1
{
public:
    int a;
    double b;
public:
    Class1(const int& a, const double& b);
    //virtual ~Class1();
    void print() const;
    double product() const;
};

Class1.cpp:

#include "stdafx.h"
#include <iostream>
#include "Class1.h"

Class1::Class1(const int& a, const double& b){
    Class1::a = a;
    Class1::b = b;
}

void Class1::print() const{
    std::cout << "a=" << Class1::a << " * b=" << Class1::b << " = " << Class1::product() << std::endl;
}

double Class1::product() const{
    return a*b;
}

Running the Matlab command mex mexTest1.cpp gives the error messages:

Creating library C:\DOCUME~1\K\LOCALS~1\TEMP\MEX_RH~1\templib.x and object C:\DOCUME~1\K\LOCALS~1\TEMP\MEX_RH~1\templib.exp 
mexTest1.obj : error LNK2019: unresolved external symbol "public: double __thiscall Class1::product(void)const " (?product@Class1@@QBENXZ) referenced in function _mexFunction 
mexTest1.obj : error LNK2019: unresolved external symbol "public: __thiscall Class1::Class1(int const &,double const &)" (??0Class1@@QAE@ABHABN@Z) referenced in function _mexFunction 
mexTest1.mexw32 : fatal error LNK1120: 2 unresolved externals 

  C:\PROGRA~1\MATLAB\R2011A\BIN\MEX.PL: Error: Link of 'mexTest1.mexw32' failed. 

??? Error using ==> mex at 208
Unable to complete successfully.

Can anyone help me fix this problem?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:48:08+00:00Added an answer on May 23, 2026 at 6:48 am

    The linker is telling you that in trying to construct an executable, it wasn’t supplied with an object file that contains Class1::product and Class1::Class1. That’s because those functions would be supplied by compiling Class1.cpp, which your command line doesn’t ask for.

    You should use the syntax of mex for multiple files: mex mexTest1.cpp Class1.cpp

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a simple class to read a csv file and store
I am trying to create a simple dialog in MFC using Visual C++. My
I've been trying to create a simple class that implements the ViewSwitcher.ViewFactory interface basing
I'm trying to create a simple date class, but I get an error on
I'm trying to create a simple keyboard from list of QtGui.QPushButton objects. class XKeyboard(QtGui.QWidget):
I'm trying to create a simple to use singleton class to connect to mysql
I am trying to create a simple visualization of what my code does. I
I'm trying to create a simple class THistory that has one procedure that takes
I am trying to create a very simple CUser class in my project, but
I am trying to create a class called LinkButton, which is a simple class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.