简体   繁体   中英

define global matrix variable in c++ with Armadillo

C++ newbie here. Does anyone know how to define a matrix as a global variable in C++ when using Armadillo?

The code will look like:

#include <iostream>
#include "armadillo"

using namespace std;
using namespace arma;

#define mat *g    

int main(){

    extern mat *g;

    mat  g << 1.0 << 2.0 << endr
           << 3.0 << 4.0 << endr;

    return;

}

A related question is what is the type of a mat variable when I pass it to a function? Should it be somefunction(mat *g) ?

I am using Microsoft Visual Studio 2012 on a Windows 7 computer.

Thanks!

Using global variables is almost always a bad idea. But to answer your question, a global matrix variable can be done as follows:

#include <iostream>
#include <armadillo>

using namespace std;
using namespace arma;

mat global_matrix;

int main(int argc, char** argv)
  {
  global_matrix << 1.0 << 2.0 << endr
                << 3.0 << 4.0 << endr;

  global_matrix.print("global_matrix:");

  return 0;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM