PatWie / CppNumericalSolvers

a lightweight header-only C++17 library of numerical optimization methods for nonlinear functions based on Eigen
MIT License
873 stars 201 forks source link

Error C2248 occurs when starting the simple sample. #131

Closed takuto2835 closed 3 years ago

takuto2835 commented 3 years ago

I tried to utilize the solver in my application, but I got error C2248. The error was 'cppoptlib::function::Function<double,-1>::Dim': unable to access private member. I don't know if this is the right way to do it, but I changed "private:" to "public:" in line 60 of "function.h" and the error disappeared.

PatWie commented 3 years ago

Could you please provide how you use the function_t ? One example is part of CI and I wonder why I haven't seen this issue (still it can be a bug).

takuto2835 commented 3 years ago

I just copied and pasted a part of simple.cc directly into the beginning of my app. It seems that this C2248 is occurring at the point corresponding to "Solver solver;" in line 63 of simple.cc.

I looked into it and it seems to be a factor of the Visual Studio 2019 compiler.

https://docs.microsoft.com/en-us/cpp/overview/cpp-conformance-improvements?view=msvc-160

Initializers for inline static data members Invalid member accesses within inline and static constexpr initializers are now correctly detected. The following example compiles without error in Visual Studio 2017, but in Visual Studio 2019 under /std:c++17 mode it raises error C2248:

struct X
{
    private:
        static inline const int c = 1000;
};

struct Y : X
{
    static inline int d = c; // VS2019 C2248: cannot access private member declared in class 'X'.
};

To avoid the error, declare the member X::c as protected:

struct X
{
    protected:
        static inline const int c = 1000;
};
PatWie commented 3 years ago

Fixed in #135