C++: expected identifier before numeric constant
I'm trying to write a small program using MTL, but I'm getting the mentioned error when I try to make a MTL Matrix a member of a class.
#include <boost/numeric/mt开发者_运维知识库l/mtl.hpp>
class myClass
{
private:
mtl::dense2D<double> Ke(6,6);
};
However, there is no problem with the same statement in main():
#include <boost/numeric/mtl/mtl.hpp>
int main(int argc, char** argv)
{
mtl::dense2D<double> Ke(6,6);
return 0;
}
I'm very new to C++, and I don't think this is really related to the MTL, but that's where the error occurred for me.
You need to do that in the constructor's initialiser list.
class myClass {
mtl::dense2D<double> Ke;
public:
myClass() : Ke(mtl::dense2D<double>(6, 6)) { }
};
Because when you declare
mtl::dense2D<double> Ke;
you're only supposed to declare it, not create it yet. This is the constructor's job in C++:
class myClass
{
public:
myClass() // constructor
: Ke(6, 6) // here we use the constructor initializer
{
}
private:
mtl::dense2D<double> Ke; // declaration
};
You can't initialize variable within the class scope, you need to do it in a constructor. Change this:
class myClass
{
private:
mtl::dense2D<double> Ke(6,6);
};
to this --
class myClass
{
public:
myClass() : Ke(6,6) { }
private:
mtl::dense2D<double> Ke;
};
精彩评论