certik / yaml-cpp

Automatically exported from code.google.com/p/yaml-cpp
MIT License
0 stars 0 forks source link

operator[] and direct as<_T> access does not work. #131

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.
I'm on a roll today :). This is new API.

This works:
{{{
    YAML::Node testNode = node[i + j*matrix.cols()];
    matrix(i,j) = testNode.as<_Scalar>();
}}}

This does not:
{{{
    matrix(i,j) = node[i + j*matrix.cols()].as<_Scalar>();
}}}

{{{
/Users/mriedel/MPI/repos/humus-sw/TeleKyb2/trunk/stacks/telekyb_common/telekyb_b
ase/include/telekyb_base/Tools/YamlConversion.hpp:108: error: expected 
primary-expression before ‘>’ token
/Users/mriedel/MPI/repos/humus-sw/TeleKyb2/trunk/stacks/telekyb_common/telekyb_b
ase/include/telekyb_base/Tools/YamlConversion.hpp:108: error: expected 
primary-expression before ‘)’ token
}}}

Adding Brackets doesn't change the Issue.

3.

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?

Please provide any additional information below.

Original issue reported on code.google.com by m...@riedel-privat.de on 13 Nov 2011 at 11:30

GoogleCodeExporter commented 9 years ago
Thanks for messing around with this!

In your compiler output, it says:

   before ‘>’ token

What is that symbol supposed to be?

Also, what's the definition of _Scalar, and what is your conversion code to 
yaml-cpp?

Original comment by jbe...@gmail.com on 13 Nov 2011 at 11:39

GoogleCodeExporter commented 9 years ago
Here is the whole definition:
{{{
template < typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, 
int _MaxCols>
struct convert< Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, 
_MaxCols> > {
    static Node encode(const Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& matrix) {
        Node node;
        return node;
    }

    static bool decode(const Node& node, Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& matrix) {

        int nSize = node.size(); // Sequence check is implicit
        if (nSize != matrix.rows() * matrix.cols()) {
            ROS_ERROR_STREAM("Unable to create Eigen::Matrix. Input is of wrong size! Matrix("<<
                    matrix.rows() <<"x"<< matrix.cols() <<") != Tokens("<< nSize <<")");
            return false;
        } else {
            // fill
            for (int i = 0; i < matrix.rows(); i++) {
                for (int j = 0; j < matrix.cols(); j++) {
                    //ROS_INFO_STREAM("Node pos: " << i*matrix.rows() + j << "Matrix: " << i << "," << j);
                    //node[i + j*matrix.cols()] >> matrix(i, j);
                    YAML::Node field = node[i + j*matrix.cols()];
                    matrix(i,j) = field.as<_Scalar>();
                    //matrix(i,j) = node[i + j*matrix.cols()].as<_Scalar>(); <- not working.
                }
            }
        }
        return true;
    }
};
}}}

The symbol is '>' and the stuff before an encoding error for quotes or 
something... (Eclipse on a Mac :) )

Original comment by m...@riedel-privat.de on 13 Nov 2011 at 11:44

GoogleCodeExporter commented 9 years ago
And _Scalar is int, double, float... But I think the issue lies somewhere else, 
because one version of the code is working. I could probably also provide a 
less complex example for the error.

Original comment by m...@riedel-privat.de on 13 Nov 2011 at 11:46

GoogleCodeExporter commented 9 years ago
I found the Problem.
This works:
matrix(i,j) = node[(int)(i + j*matrix.cols())].as<_Scalar>();

I have to cast the inner expression to int. I'm currently checking what it is 
before.

Martin

Original comment by m...@riedel-privat.de on 13 Nov 2011 at 11:58

GoogleCodeExporter commented 9 years ago
Hm ok, I thought matrix.cols evaluates as some integer. But apparently there is 
something going on behind the scenes that I do not completely understand.

cols() returns   
typedef typename internal::traits<Derived>::Index Index;

Original comment by m...@riedel-privat.de on 14 Nov 2011 at 12:08

GoogleCodeExporter commented 9 years ago
OK, interesting.

I'm surprised, then, that

{{{
YAML::Node testNode = node[i + j*matrix.cols()];
}}}

did not give you a compiler error as well.

Original comment by jbe...@gmail.com on 14 Nov 2011 at 1:49