C++: expected identifier before numeric constant

后端 未结 3 2014
花落未央
花落未央 2020-12-21 02:20

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 

        
相关标签:
3条回答
  • 2020-12-21 02:49

    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;
    };
    
    0 讨论(0)
  • 2020-12-21 02:53

    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)) { }
    };
    
    0 讨论(0)
  • 2020-12-21 03:12

    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
    };
    
    0 讨论(0)
提交回复
热议问题