1

I have written my program in c++ and needed to write a c++mex function to pass input parameter and retrieve some output values in matlab. but i have been facing some issues accessing my multidimensional array. here follows a piece of what i have written and the error message. w was declare as follows:

double **W;

W[i][j];

for  (int i=0;i < 6; i++)
{
    for (int j=0;j<6;j++)
    {
        //B[j + 6*i] = W[i][j];
        B[j + 6*i] = W[i + 6*j];     
    }
}

and when compiling i get the following error message:

cannot convert ‘double*’ to ‘double’ in assignment
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
JNW
  • 79
  • 1
  • 2
  • 8

2 Answers2

1

Your 2-D array represented as double** W needs to have space allocated. First you need to allocate each row. The code below illustrates this:

#include <iostream>

using namespace std;

int main() {

int rows = 6, cols = 6;
double **W; 

W = new double* [rows];

// allocate each row
for (int i = 0; i < rows; i++) {
    W[i] = new double [cols];
}

for (int i = 0; i < 6; i++) {
    for (int j = 0; j<6; j++) { 
        // do work here
    }
}

// clean up in the end:
if (W != nullptr) {
    for (int i = 0; i < rows; i++) {
        delete[] W[i];
    }
    delete[] W;
    W = nullptr;
}

return 0;
}
vishal
  • 2,258
  • 1
  • 18
  • 27
dspfnder
  • 1,135
  • 1
  • 8
  • 13
  • i have included the library but still get this error : RBM.cpp:587:10: error: ‘nullptr’ was not declared in this scope – JNW Oct 20 '15 at 09:35
  • You may be running a compiler that does not support C++11. Use NULL in the place of nullptr. – dspfnder Oct 20 '15 at 09:42
  • yes that works for me , you are genius :) thank you very much – JNW Oct 20 '15 at 10:16
0

The following code is wrong:

double **W;

W[i][j];

Either declare W as:

double W[i][j];

Or read: How do I declare a 2d array in C++ using new.

Your 2D array accessing syntax

B[j + 6*i] = W[i + 6*j]; 

is wrong too.

Are you trying to do this?

B[j][6*i] = W[i][6*j];// if B is 2D array

Or

B[j + 6*i] = W[i][6*j];// if B is 1D array
Community
  • 1
  • 1
mazhar islam
  • 5,561
  • 3
  • 20
  • 41
  • I'm pretty sure he isn't trying to do either. – Bartek Banachewicz Oct 20 '15 at 09:06
  • 1
    Also *please* don't recommend using `malloc`. – Bartek Banachewicz Oct 20 '15 at 09:12
  • the value of W is computed somewhere else and is a pointer to another pointer containing the values i need to retrieve in matlab (i mean stored into a variable). so i guess am trying to get a 2D array. Also am a bit new in C++ – JNW Oct 20 '15 at 09:16
  • @JNW, then after line `double **W;` what does `W[i][j];` statements means? – mazhar islam Oct 20 '15 at 09:17
  • i just tried : > B[j][6*i] = W[i + 6*j] // got this error: RBM.cpp: In function ‘void mexFunction(int, mxArray**, int, const mxArray**)’: RBM.cpp:574:17: error: invalid types ‘double[int]’ for array subscript – JNW Oct 20 '15 at 09:19
  • sorry i forgot to mentioned : > B[j + 6*i] = W[i][6*j]; // the following compile but I am having a run time Error this time. – JNW Oct 20 '15 at 09:24
  • 1
    @JNW, its very hard to guess why you get run time error because you didn't post [MCVE](http://stackoverflow.com/help/mcve). – mazhar islam Oct 20 '15 at 09:26
  • Have been trying to send the full function here but the site settings are a bit complex and keeps refusing to post it. something about post containing codes and need to be on a kind of format. – JNW Oct 20 '15 at 10:12
  • yes i get something with following as well... – JNW Oct 20 '15 at 10:18