0

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!

  • 2
    After a brief look at the armadillo web site, I realized that you are asking questions that are very basic C++ questions. Perhaps you should develop your C++ skills a little bit before jumping into using armadillo. Here's something to get started - http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. – R Sahu Jul 20 '14 at 02:08
  • Marshall Cline's [C++ FAQ](http://www.parashift.com/c++-faq/) can also be useful. – mtall Jul 20 '14 at 08:50

1 Answers1

2

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;
  }
hbrerkere
  • 1,561
  • 8
  • 12