9 Replies - 448 Views - Last Post: 05 August 2014 - 07:54 AM Rate Topic: -----

#1 sanann  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-August 14

how do i assign pointers for boost matrix

Posted 04 August 2014 - 07:08 AM

I am using boost matrix for generating matrices used for filters. I wan to assign a pointer to these matrices so i can call them out of the function. but i am not able to understand where i am failing. the example code is here.

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
   identity_matrix<double> M(3);
    std::cout<<M<<std::endl;

    
      double *p;
      p = &M;


      for (int i=0; i<2; i++)
      {
          for (int j=0; j<2; j++)
          {
              *(p+(i*j))=1;
      }
      }
      for (int i=0; i<2; i++)
      {
          for (int j=0; j<2; j++)
          {
              std::cout << numbers[i][j] << std::endl;
      }
      }

      return 0;

This post has been edited by JackOfAllTrades: 04 August 2014 - 07:29 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: how do i assign pointers for boost matrix

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4132
  • View blog
  • Posts: 12,852
  • Joined: 25-December 09

Re: how do i assign pointers for boost matrix

Posted 04 August 2014 - 08:31 AM

Why are you trying to use a pointer to this class? Why not just use one of the class member functions designed to be used for this class? Did you even try reading some documentation for this non-standard class? Perhaps you could use the overload of operator() instead of the pointer.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 sanann  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-August 14

Re: how do i assign pointers for boost matrix

Posted 05 August 2014 - 12:02 AM

View Postjimblumberg, on 04 August 2014 - 08:31 AM, said:

Why are you trying to use a pointer to this class? Why not just use one of the class member functions designed to be used for this class? Did you even try reading some documentation for this non-standard class? Perhaps you could use the overload of operator() instead of the pointer.

Jim


hello I am new to c++, i just have basic knowledge and know that by using pointers i can get multiple outputs from a function, so i thought that would help me. but i am afraid i am stuck up in a situation where i cannot use pointers.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: how do i assign pointers for boost matrix

Posted 05 August 2014 - 06:10 AM

To get multiple outputs from a function you have to use pointers in C, but not in C++. The preference in C++ is to use references instead of pointers (even though under the covers they almost devolve to the same thing).

In C:
void ComputeXYZ(int foo, int * x, int * y, int * z);



In C++:
void ComputeXYZ(int foo, int & x, int & y, int & z);



And when you have many many parameters, it's best to package them up into a structure, and then pass the structure around.
Was This Post Helpful? 0
  • +
  • -

#5 sanann  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-August 14

Re: how do i assign pointers for boost matrix

Posted 05 August 2014 - 06:14 AM

thank you for the info but my situation is like this
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

using namespace boost;
using namespace boost::numeric::ublas;


typedef boost::numeric::ublas::matrix<double> BMT;

BMT fun(int w,int h)
  {

    BMT M[3]=
          {
        scalar_matrix<double>(w, h),
        scalar_matrix<double>(w+1, h+1),
        scalar_matrix<double>(w+2, h+2)
    };
  //  std::cout<<M[0]<<std::endl;
  return M;
  }


now i am interested in array M, the above code i get an error that boost cannot be converted into int so could you please suggest me where i am making a mistake?

This post has been edited by Skydiver: 05 August 2014 - 06:17 AM
Reason for edit:: Put code in code tags. Learn to do this yourself.

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: how do i assign pointers for boost matrix

Posted 05 August 2014 - 06:40 AM

Post the exact error message. Your report that "boost cannot be converted into int" does not make sense. It seems like you dropped some important words.
Was This Post Helpful? 0
  • +
  • -

#7 sanann  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-August 14

Re: how do i assign pointers for boost matrix

Posted 05 August 2014 - 06:43 AM

View PostSkydiver, on 05 August 2014 - 06:40 AM, said:

Post the exact error message. Your report that "boost cannot be converted into int" does not make sense. It seems like you dropped some important words.

the error i get is

error: could not convert '(BMT*)(& M)' from 'BMT* {aka boost::numeric::ublas::matrix<double>*}' to 'BMT {aka boost::numeric::ublas::matrix<double>}'
return M;
^
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4132
  • View blog
  • Posts: 12,852
  • Joined: 25-December 09

Re: how do i assign pointers for boost matrix

Posted 05 August 2014 - 06:44 AM

Quote

now i am interested in array M, the above code i get an error that boost cannot be converted into int so could you please suggest me where i am making a mistake?

The first mistake you're making is thinking M is an array, it's not an array, it's a boost::numeric::ublas::matrix<double>. This class has member functions to access the elements of the matrix along with constructors to properly construct the class. Did you read the documentation I provided in my last link?

If you're just learning C++ perhaps this class is too complicated and you may need to start simpler. It seems to me that you don't yet have a good grasp on using C++ classes and maybe you need to start by studying how basic classes work.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 sanann  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-August 14

Re: how do i assign pointers for boost matrix

Posted 05 August 2014 - 06:50 AM

the documentation you provided was quite helpful.my knowledge with classes is very limited..I wanted to know if there was a possible way to obtain the matrices stored in matrix M to further use them separetely else where.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4132
  • View blog
  • Posts: 12,852
  • Joined: 25-December 09

Re: how do i assign pointers for boost matrix

Posted 05 August 2014 - 07:54 AM

Quote

I wanted to know if there was a possible way to obtain the matrices stored in matrix M to further use them separetely else where.

Yes, but you must return the proper type of variable.

Why are you trying to create an array of boost::numeric::ublas::matrix<double> in your function?

Why are you trying to return this array from a function that expects to return a single instance of boost::numeric::ublas::matrix<double>? By the way trying to return a static array that has local scope will not work as expected. If you actually want an array, I suggest you consider using a std::vector instead.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1