This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Return a vector from a function

Tags: None
(comma "," separated)
liamcattell
Registered Member
Posts
2
Karma
0

Return a vector from a function

Mon Oct 24, 2011 6:47 pm
Hi

I'm new to eigen, and fairly new to C++. As a result, I've hit a bit of a problem. I want to return a vector from a function, but I'm not really sure if it's even possible. I know that returning an array in C++ is a nasty business, but I wondered if there's a robust solution.

My current attempt throws up this error:
Unhandled exception at 0x770b15ee (ntdll.dll) in Test project 3.exe: 0xC0000005: Access violation writing location 0x00000000.

I have a class called cThinPlateSpline, which has the following method:
Code: Select all
VectorXf cThinPlateSpline::fvflGetHomolog(const MatrixXf& mflLandmarksIn, const MatrixXf& mflHomologsIn, const VectorXf& vflPoint)
{
  ...

  // Create the vector to return from the function
  VectorXf vflHomologOfPoint;
  vflHomologOfPoint.setZero();

  // Some maths to set the actual values of vflHomologOfPoint
  ...

  return vflHomologOfPoint;
}


I would like to return vflHomologOfPoint, but the above code doesn't work.

The main function looks like this:
Code: Select all
int _tmain(int argc, _TCHAR* argv[])
{
  // Initialise Nx2 floats matrix of landmark co-ordinates
  // Note: mflLandmarks = [x1 y1 (z1); x2 y2 (z2); ...; xn yn (zn)]
  MatrixXf mflLandmarks(4,2);

  // Initialise MxN floats matrix of homologous warped co-ordinates
  // Note: mflHomologs = [x1 x2 ... xn; y1 y2 ... yn; (z1 z2 ... zn)]
  MatrixXf mflHomologs(2,4);

  // Set up landmarks
  mflLandmarks <<  0,  1,
        -1,  0,
         0, -1,
         1,  0;

  // Set up homologs
  mflHomologs << 0,   -1,     0,    1,
       0.75, 0.25, -1.25, 0.25;
   
  cThinPlateSpline oTPS;
   
  VectorXf vflPoint;
  vflPoint <<  0.5,
              -0.5;

  VectorXf vflHomologOfPoint(2);
  vflHomologOfPoint << oTPS.fvflGetHomolog(mflLandmarks, mflHomologs, vflPoint);

  // Pause the output   
  cin.get();
 
  return 0;
}


Any help, or pointers (pun intended), would be much appreciated.

Thanks in advance.

Liam
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Return a vector from a function

Tue Oct 25, 2011 7:25 am
You probably forgot to resize the vector vflHomologOfPoint. If you try it in "debug" mode, you should get a more explicit error message.
liamcattell
Registered Member
Posts
2
Karma
0

Re: Return a vector from a function

Tue Oct 25, 2011 10:23 pm
Thanks! You were right, I'd forgotten to resize the vector.

Liam


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]