简体   繁体   English

错误信息:非标准语法; 使用“&”创建指向成员的指针

[英]Error Message: non-standard syntax; use '&' to create a pointer to member

I want to try the test code from the OptimLib page (link)我想尝试来自 OptimLib 页面的测试代码(链接)

OptimLib优化库

This is the code from the optimlib page:这是来自 optimlib 页面的代码:

i just changed the code a little bit.我只是稍微更改了代码。 The following function is now a normal function that you can call instead of the original "main" function以下函数现在是您可以调用的普通函数,而不是原来的“main”函数

void optimizationApp::optimTest()
{

// initial values:
arma::vec x = arma::ones(2,1) + 1.0; // (2,2)

//

std::chrono::time_point<std::chrono::system_clock> start = std::chrono::system_clock::now();

bool success = optim::de(x,ackley_fn,nullptr);

std::chrono::time_point<std::chrono::system_clock> end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;

if (success) {
    std::cout << "de: Ackley test completed successfully.\n"
              << "elapsed time: " << elapsed_seconds.count() << "s\n";
} else {
    std::cout << "de: Ackley test completed unsuccessfully." << std::endl;
}

arma::cout << "\nde: solution to Ackley test:\n" << x << arma::endl;

}

The following function is the optimization function:下面的函数是优化函数:

double optimizationApp::ackley_fn(const arma::vec& vals_inp, arma::vec* grad_out, void* opt_data)
{
const double x = vals_inp(0);
const double y = vals_inp(1);
const double pi = arma::datum::pi;

double obj_val = -20*std::exp( -0.2*std::sqrt(0.5*(x*x + y*y)) ) - std::exp( 0.5*(std::cos(2*pi*x) + std::cos(2*pi*y)) ) + 22.718282L;

//

return obj_val;
}

But when i want to compile the code, the error message appears in this line:但是当我想编译代码时,错误信息出现在这一行中:

bool success = optim::de(x,ackley_fn,nullptr);

error: reference to non-static member function must be called optimizationApp::ackley_fn': non-standard syntax;错误:必须调用非静态成员函数的引用 optimizationApp::ackley_fn': 非标准语法; use '&' to create a pointer to member使用“&”创建指向成员的指针

The two test function i use, are defined in the header of my software我使用的两个测试函数在我的软件的标题中定义

        void optimTest();
        double ackley_fn(const arma::vec& vals_inp, arma::vec* grad_out, void* opt_data);

What did i wrong, has anybody an idea?我做错了什么,有人知道吗? I don't know how to fix the error.我不知道如何修复错误。

I would be pleased, if someone can give me a tip我会很高兴,如果有人能给我小费

Best wishes and stay healthy during the pandemic祝愿疫情期间身体健康

I created a new project and copied the code 1 to 1 into it.我创建了一个新项目并将代码 1 到 1 复制到其中。 In the Pro file (I use Qt and MSVC2015x64) I added Armadillo and the OptimLib.在 Pro 文件(我使用 Qt 和 MSVC2015x64)中,我添加了 Armadillo 和 OptimLib。

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

DEFINES += QT_DEPRECATED_WARNINGS


SOURCES += \
    main.cpp

 INCLUDEPATH +=  optimlib \                             
            armadillo-7.800.1/include

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

Now comes the following error:现在出现以下错误:

error: C2039: 'function': is not a member of 'std'错误:C2039:“function”:不是“std”的成员

I then included "functional" and the error was gone.然后我包含了“功能性”并且错误消失了。 As a result, there are now errors in functional itself.结果,现在功能本身存在错误。

I do not understand why ?我不理解为什么 ? I had built the Header Only Library in Linux and copied it to my Windows system, but they are only ".hpp" files.我已经在 Linux 中构建了 Header Only 库并将其复制到我的 Windows 系统中,但它们只是“.hpp”文件。 So this should work independently of a compiler or ?所以这应该独立于编译器或? The Armadillo Library I use is also header only and is located in the workspace of my program, like OptimLIb我使用的犰狳库也只是标题,位于我的程序的工作区中,如 OptimLIb

How do I get the library to run?如何让库运行? Or does the library not work at all under my conditions ?或者图书馆在我的条件下根本不起作用?

Put your libraries in your workspace.将您的库放在您的工作区中。 Do not change anything on the libraries themselves.不要更改库本身的任何内容。 Include them in your software.将它们包含在您的软件中。 Before you include the optimlib, include the default libraries that are missing.在包含 optimlib 之前,请包含缺少的默认库。

#include <functional>
#include <chrono>
....
#include "armadillo"
#include "optim"

That should be it.应该是这样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 非标准语法; 使用“&”创建指向成员的指针 - non-standard syntax; use '&' to create a pointer to member 非标准语法; 使用 '&amp;' 创建指向成员 C++ 的指针 - non-standard syntax; use '&' to create a pointer to member C++ 非标准语法,使用 &amp; 创建指向成员的指针 - Non-standard syntax, use & to create a pointer to member 非标准语法; 将成员函数分配给vector时,使用&#39;&&#39;创建指向成员错误的指针 - non-standard syntax; use '&' to create a pointer to member error when assigning member function to vector Visual Studio 2015 中的“非标准语法;使用‘&amp;’创建指向成员的指针”错误 - "non-standard syntax; use '&' to create a pointer to member" error in Visual Studio 2015 了解重载运算符。 获得“非标准语法”; 使用“&”创建指向成员的指针”错误 - Learning about Overloading Operators. Getting “non-standard syntax; use '&' to create a pointer to a member” error C3867:&#39;_com_error :: Description&#39;:非标准语法 使用“&”创建指向成员的指针 - C3867 : '_com_error::Description': non-standard syntax; use '&' to create a pointer to member std::unordered_map 中的 std::function,错误 C3867 非标准语法; 使用 '&amp;' 创建指向成员的指针 - std::function in std::unordered_map, error C3867 non-standard syntax; use '&' to create a pointer to member “错误C3867:非标准语法; 使用虚拟流操纵器时,使用“&”创建指向成员的指针” - “Error C3867: non-standard syntax; use '&' to create a pointer to member” when using virtual stream manipulator Visual Studio 2015 错误 C3867 &#39;Rectangle::get_area&#39;:非标准语法; 使用“&amp;”创建指向成员的指针 - Visual Studio 2015 Error C3867 'Rectangle::get_area': non-standard syntax; use '&' to create a pointer to member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM