VS2017“非标准语法;使用“&"创建一个指向成员的指针 [英] VS2017 "non-standard syntax; use '&' to create a pointer to member "

查看:27
本文介绍了VS2017“非标准语法;使用“&"创建一个指向成员的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class DefInt
{
private:
    double a;
    double b;
    double (*f)(double x);
    int N;
public:
    DefInt(double c, double d, double (*g)(double y))
    {
        a = c;
        b = d;
        f = g;
    }

    double BySimpson()
    {
        double sum = f(a) + 4 * f((a + b) / 2) + f(b);
        return sum * (b - a) / 3;
    }

};
double g(double y)
{
    double sum = 1 - y * y + y * y * y;
    return sum;
}
int main()
{
    int c = 1;
    int d = 2;
    double y;
    DefInt MyInt(c, d, g);
    cout << "BySimpson:" << MyInt.BySimpson << endl << endl;
    system("pause");
    return 0;
}

为什么会出现DefInt::BySimpson"错误:非标准语法;使用&"创建一个指向成员的指针?顺便说一下,我省略了一个类似的 DefInt 成员函数,虽然它与 Bysimpson 几乎相同,但它工作正常并且不会发生错误.我不理解为什么.我把它附在这里.

why is there a error saying 'DefInt::BySimpson': non-standard syntax; use '&' to create a pointer to member? By the way I ommited a similar DefInt member function,though it is nearly the same as Bysimpson, it works fine and no error occurs. I do not understand why. I have attched it here.

double ByTrapzold(int n)
{
    N = n;
    double sum = f(a + (b - a) / N);
    for (int i = 2; i <= N; i++)
    {
        sum = sum + 2 * f(a + (b - a) * i / N);
    }
    sum = sum + f(a + (b - a) * (N + 1) / N);
    return sum * (b - a) / (2 * N);
}

谢谢.

推荐答案

上线

cout << "BySimpson:" << MyInt.BySimpson << endl << endl;

您可能打算打电话给 BySimpson,但您忘记了 ()

You probably meant to make a call to BySimpson but your forgot the ()

cout << "BySimpson:" << MyInt.BySimpson() << endl << endl;

你得到这个误导性错误的原因是因为预 ISO 标准化 MyInt.BySimpson 实际上意味着你想要的地址就像普通函数一样,函数名称本身给出了函数的地址.然而后来使用 &将成员的地址作为要求写入标准.因此 Visual Studio 认为您仍在使用旧语法并希望您使用新语法.

The reason you get this misleading error is because pre ISO standarization MyInt.BySimpson would actually mean you wanted the address just like for normal function the function name on its own gives the address of the function. Later however the use of & to take the address of a member was put in the standard as a requirement. So Visual Studio thinks you are still using the old syntax and wants you to use the new syntax.

这篇关于VS2017“非标准语法;使用“&amp;"创建一个指向成员的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆