C ++: expected identifier before a numeric constant

I am trying to write a small program using MTL, but I get the indicated error when I try to make MTL Matrix a member of the class.

#include <boost/numeric/mtl/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 am very new to C ++ and I don't think it really is related to MTL, but where the error occurred to me.

+3
source share
3 answers

You need to do this in the constructor initializer list.

class myClass {
    mtl::dense2D<double> Ke;
public:
    myClass() : Ke(mtl::dense2D<double>(6, 6)) { }
};
+6
source

Because when you announce

mtl::dense2D<double> Ke;

you should only declare , but not create it yet. This is the constructor job in C ++:

class myClass
{
public:
    myClass() // constructor
        : Ke(6, 6) // here we use the constructor initializer
    {
    }
private:
    mtl::dense2D<double> Ke; // declaration
};
+4
source

You cannot initialize a variable within a class; you need to do this in the constructor. Change this:

class myClass
{
private:
    mtl::dense2D<double> Ke(6,6);
};

for this -

class myClass
{
public:
    myClass() : Ke(6,6) { }
private:
    mtl::dense2D<double> Ke;
};
+3
source

All Articles