0

I would like to declare a specific boost ublas vector as global variable. The problem is that declaration outside of a function always leads to an error.

Here is a specific example:

The following code will give multiple errors: (error C2143: syntax error : missing ';' before '<<=' error C4430: missing type specifier - int assumed. error C2371: 'test' : redefinition; different basic types)

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/assignment.hpp> 
using namespace boost::numeric::ublas;

vector<int> test(3);
test <<= 1,2,3;

void main () {
std::cout << test << std::endl;
}

Moving the declaration to the main program however works

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/assignment.hpp> 
using namespace boost::numeric::ublas;

vector<int> test(3);

void main () {
test <<= 1,2,3;
std::cout << test << std::endl;
}
Ankur
  • 5,086
  • 19
  • 37
  • 62

2 Answers2

1

Of course it leads to error, since it's

test.operator <<= (1,2,3);

but you cannot call functions outside of functions.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
0

In C++11 this can be solved with a lambda:

const auto test = [](){
    ublas::vector<int> m(3);
    m <<= 1, 2, 3;
    return m;
}();