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

Beginner's: how do I pass a default vector?

Tags: None
(comma "," separated)
marcostamazza
Registered Member
Posts
2
Karma
0
hello,
I would like to pass an integer as default VectorXi argument as function argument, so I would like to define the int as a vector of size 1.
I tried different things, for example comma initializer or
Code: Select all
void chk(Eigen::VectorXd a =  VectorXd::Constant(1, 1.0),
                    Eigen::VectorXi b =  VectorXi::Constant(1, 0)){
  cout << a.size() << " a is 1.0 = " << a << "  ";
  cout << b.size() << " b is 0 = " << b << endl;
}

for example but i get warnings
Warning messages:
1: Unable to parse C++ default value 'VectorXd::Constant(1, 1.0)' for argument a of function chk
2: Unable to parse C++ default value 'VectorXi::Constant(1, 0)' for argument b of function chk

passing
Code: Select all
chk(Eigen::VectorXd a =  1.0,  Eigen::VectorXi b =  0)

actually works, but what if I want to pass a vector with two elements?

Any help appreciated. Thanks, Marco
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Your first example is perfectly fine, except that you're missing the Eigen:: namespace prefix.

Also, I guess that those warnings come from your IDE, and not from the compiler. In general don't believe to much in those warnings as IDE's parsers are rather limited regarding templates.
marcostamazza
Registered Member
Posts
2
Karma
0
ggael wrote:Your first example is perfectly fine, except that you're missing the Eigen:: namespace prefix.

Also, I guess that those warnings come from your IDE, and not from the compiler. In general don't believe to much in those warnings as IDE's parsers are rather limited regarding templates.


Thanks ggael. Then the problem is not Eigen.
I should have specified I am using RcppEigen in Rstudio. Maybe it depends on that, it is difficult for me to now if thewarnings and errors come from the compiler. What happens is that it compiles giving warnings
Code: Select all
void chk(Eigen::VectorXd a = Eigen::VectorXd::Constant(2, 0.0),
                    Eigen::VectorXi b = Eigen::VectorXd::Constant(2, 1)){
  cout << a.size() << " a is 1.0 = " << a << "  ";
  cout << b.size() << " b is 0 = " << b << endl; 
}

and when I run chk() I get the following error
Error in chk() : argument "a" is missing, with no default


maybe i should ask the Rcpp community.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
ah, right, this might rather be an issue with Rcpp.


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot]