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

namespace problem with Eigen

Tags: None
(comma "," separated)
hijokpayne
Registered Member
Posts
25
Karma
0

namespace problem with Eigen  Topic is solved

Wed Oct 17, 2012 12:15 am
Hi all,
I should start by saying that I have recently started using Eigen, and it has been really great anf FAST.

I am trying to write a small code to read back Eigen matrices from a file.
I have written this small header to overload >> to read matrix from std::istream as following:

EigenUtils.h
Code: Select all
#ifndef EIGEN_UTILS_H
#define EIGEN_UTILS_H

#include <Eigen/Dense>
#include <fstream>
#include <iostream>

namespace Eigen {

template <typename Derived>
inline std::istream& operator>>(std::istream& is, MatrixBase<Derived> const & rhs)
{
  typedef typename Derived::Scalar Scalar;   
  Scalar data[rhs.size()]; 
  for(int i = 0; i < rhs.size(); ++i)
        is >> data[i];
       
  const_cast< MatrixBase<Derived>& >(rhs) =
      Map< Matrix<Scalar, Dynamic, Dynamic, RowMajor> > (data, rhs.rows(), rhs.cols());
 
  return is;
}

}
#endif // EIGEN_UTILS_H


And my main.cpp
Code: Select all
#include <Eigen/Dense>
#include <iostream>
#include <fstream>

// This works
namespace Eigen {
#include "EigenUtils.h"
}

// This DOES NOT work
//#include "EigenUtils.h"

int main() {
  Eigen::Matrix3d m_in;
  m_in.setRandom();
  std::cout << "m_in=\n" << m_in << std::endl;
 
  // Now save matrix m in file.txt
 
  std::string filename("file.txt");
  {
    std::ofstream file(filename.c_str());
    if(file.is_open()) {
      file << m_in;
      file.close();
    }
  }
 
  // Now Read back   
  {
    Eigen::Matrix3d m_out;
    std::ifstream file(filename.c_str());
    if(file.is_open()) {
      file >> m_out;   
      file.close();
      std::cout << "m_out=\n" << m_out << std::endl; 
    }
    else
      std::cout << "Opening " << filename << " failed .." << std::endl;     
  }
 
   return 0;
}


It works if I directly put the definition in my main file, or use namespace Eigen {#include "EigenUtils.h"} . Otherwise,just simply doing #include"EigenUtils.h" starts giving error like: ‘Dynamic’ was not declared in this scope. The compiler nicely suggests using "Eigen::Dynamic",but that does not help.

Is there a way, I can avoid this? I am having hard time figuring where I am doing wrong. I will really appreciate any help regarding this.


Update
I am sorry. This actually works. I was editing one header and compiling a unedited one :)


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]