-3
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;
}

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);
}

Thanks.

user4581301
  • 33,082
  • 7
  • 33
  • 54
Yimeng Ma
  • 3
  • 1
  • 2
  • 4
    You're missing a set of `()`. `cout << "BySimpson:" << MyInt.BySimpson << endl << endl;` should be `cout << "BySimpson:" << MyInt.BySimpson() << endl << endl;` – NathanOliver Oct 04 '17 at 16:59
  • Thanks. I just began to learn and I thought it was ok to not put a () if no variables are needed. – Yimeng Ma Oct 04 '17 at 17:08
  • @YimengMa Why would you ever think that? [Read a good book on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of trying to learn by coding randomly. – Algirdas Preidžius Oct 04 '17 at 17:14
  • well in fact I am taking a course and this is a homework.I know this question was silly but I cant delete it since it has been answered. How can I delete it now? I mean I understand it is not helpful to others and I dont want it to appear on my homepage. – Yimeng Ma Oct 04 '17 at 17:20
  • @YimengMa 1) If it is your homework, the source material should have been, already, explained to you, so you should've, already, known the answer. 2) You can't. Unless the person who answered your question deletes it, and your question goes back to not being answered. – Algirdas Preidžius Oct 04 '17 at 17:24
  • @AlgirdasPreidžius -- the reason that someone would think that is that that's how it's done in some languages. Pascal, for example. – Pete Becker Oct 04 '17 at 17:35
  • @PeteBecker Yes, I know that it's done like that in other languages (I am using PL/SQL from time to time, which shares such functionality as well), but why would one assume, that several, separate, languages must share the same syntax? – Algirdas Preidžius Oct 04 '17 at 17:38
  • @AlgirdasPreidžius -- seriously? You've never tried something that you thought would work and been surprised that it didn't? – Pete Becker Oct 04 '17 at 17:39
  • @PeteBecker Typically, when some language feature, that I tried to guess how it worked in a specific language, didn't work, I consulted the documentation of a language. And no, I never was surprised, that it didn't - I knew, upfront, that I was just making a guess, and it might not work because of that fact. – Algirdas Preidžius Oct 04 '17 at 17:50
  • @AlgirdasPreidžius -- perhaps you've forgotten: your question was "Why would you ever think that?" I answered. Now you're arguing that **you** wouldn't think that. Maybe; you might be the most wonderful software engineer in the world, never making incorrect assumptions. Please allow those of us who are less perfect to be merely human. – Pete Becker Oct 04 '17 at 18:36
  • @PeteBecker And your question was "_You've never tried something that you thought would work and **been surprised** that it didn't?_", to which I answered that, yes, I did, sometimes, make incorrect assumptions, but, because I knew, that they were just that - assumptions (=guesses), I was never surprised at the fact that they didn't work. So, please explain, where "_never making incorrect assumptions_" came from. – Algirdas Preidžius Oct 04 '17 at 21:02

1 Answers1

4

On the line

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

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

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

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.

Eelke
  • 20,897
  • 4
  • 50
  • 76
  • 3
    Please don't answer typo questions. We have close vote reason for them. – NathanOliver Oct 04 '17 at 17:07
  • Thanks. I just began to learn and I thought it was ok to not put a () if no variables are needed. – Yimeng Ma Oct 04 '17 at 17:09
  • `MyInt.BySimpson` **never** meant that you wanted the address of the function, except in Microsoft's imagination. It was never part of the language. Microsoft informally circulated a paper suggesting that the standards committee endorse their compiler's bug; the paper got a cumulative shrug, and it quickly disappeared. – Pete Becker Oct 04 '17 at 17:38