Error in Matrix Calculation Program Using std::vector and Eigen::MatrixXd

Asked 2 years ago, Updated 2 years ago, 33 views

I'd like to add Eigen::MatrixXd to std::vector and create a program to calculate the matrix in vector later, but it just won't work...
Running the program below will result in this error.

affine.cpp:In function 'int main(int, const char**)':
affine.cpp:15:12:error:'itr' does not name a type
   for(autoitr=model.begin();itr!=model.end();++itr){
            ^
affine.cpp:15:33:error:expected'; 'before'itr'
   for(autoitr=model.begin();itr!=model.end();++itr){
                                 ^
affine.cpp:15:33:error:'itr'was not decoded in this scope

The source code is from here.

#include<iostream>
# include <vector>
# include "Eigen/Core"

int main(intargc, char const*argv[])
{
  std::vector<Eigen::MatrixXd>model;

  model.push_back(Eigen::MatrixXd(3,6));
  model.push_back(Eigen::MatrixXd(6,5));

  Eigen::MatrixXda(1,3);
  std::cout<<a<<std::endl;

  for(autoitr=model.begin();itr!=model.end();++itr){
    a*=(*itr);
  }

  return 0;
}

In addition to how to resolve the error, I would appreciate it if you could let me know if it is okay to push back MatrixXd to vector later.
Please let me know if anyone knows.

c++

2022-09-30 17:09

2 Answers

The compiler seems to be using g++, but auto was introduced from C++11 and requires -std=c++11 for the compilation option.Without it, such an error occurs.

Will it be okay to push back MatrixXd to vector later?

I think it's okay if that means whether it works or not.When the vector grows in size, it copies or moves the contents, so I'm concerned about its performance, but if you look at the header file roughly, it seems that it supports move constructors, so you don't have to be nervous.

By the way, if you want to push_back as soon as you create an instance, you can use emplace_back.

model.push_back(Eigen::MatrixXd(3,6));
  model.push_back(Eigen::MatrixXd(6,5));

instead of
model.emplace_back(3,6);
  model.emplace_back(6,5);

Let's say.It looks great and can improve performance.


2022-09-30 17:09

Although Hideki answered most of them, the auto keyword originally represented the storage class and was treated in the same column as static, extern, typedef, register.

auto inti;
static intj;

Therefore,

affine.cpp:15:12:error:'itr' does not name a type
for(autoitr=model.begin();itr!=model.end();++itr){
         ^

auto followed by the type name as indicated by the error

C++11 changes the meaning of auto and can be treated as an automatic variable, but to do so, you must specify that the source code is C++11.

Also, in the second half,

for(autoitr=model.begin();itr!=model.end();++itr){
  a*=(*itr);
}

where std::accumulate, std::begin, std::multiplies , Use uniform initialization

 a=accumulate(begin(model), end(model), a, std::multiplies<>{});

can be written as


2022-09-30 17:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.