function call misinterpreted as a variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sjbrown8@eng.usf.edu

    function call misinterpreted as a variable

    I have the piece of code below, and when i try compiling with the line

    g++ 753075304.cpp

    I get the following error message:

    753075304.cpp: In function 'int main()':
    753075304.cpp:2 9: error: 'plus' was not declared in this scope

    I've gone over the code several times line by lin, and my eyes are
    starting to bleed. It looks totally fine to me, and I'm actually
    wondering whether I found a compiler bug, but more likely some weird
    feature, or I'm just totally missing something. And before anyone asks,
    yes it is homework, but I am trying to grade it, not do it. my version
    of GCC (as reported by its man page) is 4.0.1 2005-07-11 and I am
    running debian testing, kernel version 2.6.8-1-386 #1

    #include <iostream>
    using namespace std;
    #include <cmath>
    double quad(double, double, double);
    double plus(double, double, double);
    double neg(double, double, double);

    int main()
    {
    double xpos;
    double a;
    double b;
    double c;
    double square;
    double xneg;

    cout << "This program will compute a quadratic equation\n";
    cout << "Please enter a number: \n";
    cin >> a;
    cout << "Please enter another number: \n";
    cin >> b;
    cout << "Please enter a final number: \n";
    cin >> c;

    square = quad(a,b,c);
    xneg = neg(a,b,square) ;
    xpos = plus(a,b,square );

    cout << "The positive function is: " << xpos << "\n";
    cout << "The negative function is: " << xneg << "\n";

    return 0;
    }

    double quad(double n1, double n2, double n3)
    {
    double y;
    double z;
    double w;
    double root;

    y = 4*n1*n3;
    z = n2 * n2;
    w = z-y;
    root = sqrt(w);
    return root;
    }

    double plus( double n1, double n2, double n3)
    {
    double x;

    x= (-1 * n2 + n3)/(2*n1);
    return x;
    }

    double neg(double n1, double n2, double n3)
    {
    double x;

    x= (-1 * n2 - n3)/(2*n1);
    return x;
    }
  • Zorro

    #2
    Re: function call misinterpreted as a variable

    I compiled it in visual studio, and it had no errors.
    Sorry, for not having better news. It is a GCC bug.

    Regards,
    zorabi@ZHMicro. com




    Comment

    • Zara

      #3
      Re: function call misinterpreted as a variable

      sjbrown8@eng.us f.edu wrote:[color=blue]
      > I have the piece of code below, and when i try compiling with the line
      >
      > g++ 753075304.cpp
      >
      > I get the following error message:
      >
      > 753075304.cpp: In function 'int main()':
      > 753075304.cpp:2 9: error: 'plus' was not declared in this scope
      >[/color]
      (...)[color=blue]
      >
      > #include <iostream>
      > using namespace std;
      > #include <cmath>
      > double quad(double, double, double);
      > double plus(double, double, double);
      > double neg(double, double, double);
      >
      > int main()
      > {
      > double xpos;
      > double a;
      > double b;
      > double c;
      > double square;
      > double xneg;
      >
      > cout << "This program will compute a quadratic equation\n";
      > cout << "Please enter a number: \n";
      > cin >> a;
      > cout << "Please enter another number: \n";
      > cin >> b;
      > cout << "Please enter a final number: \n";
      > cin >> c;
      >
      > square = quad(a,b,c);
      > xneg = neg(a,b,square) ;
      > xpos = plus(a,b,square );
      >
      > cout << "The positive function is: " << xpos << "\n";
      > cout << "The negative function is: " << xneg << "\n";
      >
      > return 0;
      > }[/color]
      (...)

      I tried to compile it with GCC 3.4.2, adn it also failed.
      The problem lies in a existing template version of plus in std
      namespace. I think it is a GCC bug, because AFAIK the compiler *should*
      prefer non-template functions, but anyhiw, there is a workaround:

      substitute your line
      using namespace std;
      with these two lines
      using std::cin;
      usign std::cout;

      In general, you should try to limit your using clauses to specific
      classes or functions, avoiding using complete namespaces.

      Best regards

      Comment

      • Kyle

        #4
        Re: function call misinterpreted as a variable

        Zara wrote:[color=blue]
        > sjbrown8@eng.us f.edu wrote:
        >[color=green]
        >> I have the piece of code below, and when i try compiling with the line
        >>
        >> g++ 753075304.cpp
        >>
        >> I get the following error message:
        >>
        >> 753075304.cpp: In function 'int main()':
        >> 753075304.cpp:2 9: error: 'plus' was not declared in this scope
        >>[/color]
        > (...)
        >[color=green]
        >>
        >> #include <iostream>
        >> using namespace std;
        >> #include <cmath>
        >> double quad(double, double, double);
        >> double plus(double, double, double);
        >> double neg(double, double, double);
        >>
        >> int main()
        >> {
        >> double xpos;
        >> double a;
        >> double b;
        >> double c;
        >> double square;
        >> double xneg;
        >>
        >> cout << "This program will compute a quadratic equation\n";
        >> cout << "Please enter a number: \n";
        >> cin >> a;
        >> cout << "Please enter another number: \n";
        >> cin >> b;
        >> cout << "Please enter a final number: \n";
        >> cin >> c;
        >>
        >> square = quad(a,b,c);
        >> xneg = neg(a,b,square) ;
        >> xpos = plus(a,b,square );
        >>
        >> cout << "The positive function is: " << xpos << "\n";
        >> cout << "The negative function is: " << xneg << "\n";
        >>
        >> return 0;
        >> }[/color]
        >
        > (...)
        >
        > I tried to compile it with GCC 3.4.2, adn it also failed.
        > The problem lies in a existing template version of plus in std
        > namespace. I think it is a GCC bug, because AFAIK the compiler *should*
        > prefer non-template functions, but anyhiw, there is a workaround:[/color]

        seems unlikely to me that its a bug, as Comeau is giving following error
        for that code

        "ComeauTest .c", line 27: error: "plus" is ambiguous
        xpos = plus(a,b,square );

        Comeau could by buggy as well, but i wouldnt bet on it(, at last without
        long session with standard and rules of lookup and function choosing)
        [color=blue]
        >
        > substitute your line
        > using namespace std;
        > with these two lines
        > using std::cin;
        > usign std::cout;
        >
        > In general, you should try to limit your using clauses to specific
        > classes or functions, avoiding using complete namespaces.
        >
        > Best regards[/color]

        Comment

        • Zara

          #5
          Re: function call misinterpreted as a variable

          Kyle wrote:[color=blue]
          > Zara wrote:[color=green]
          >> sjbrown8@eng.us f.edu wrote:
          >>[color=darkred]
          >>> I have the piece of code below, and when i try compiling with the line
          >>>
          >>> g++ 753075304.cpp
          >>>
          >>> I get the following error message:
          >>>
          >>> 753075304.cpp: In function 'int main()':
          >>> 753075304.cpp:2 9: error: 'plus' was not declared in this scope
          >>>[/color]
          >> (...)
          >>[color=darkred]
          >>>
          >>> #include <iostream>
          >>> using namespace std;
          >>> #include <cmath>
          >>> double quad(double, double, double);
          >>> double plus(double, double, double);
          >>> double neg(double, double, double);
          >>>
          >>> int main()
          >>> {
          >>> double xpos;
          >>> double a;
          >>> double b;
          >>> double c;
          >>> double square;
          >>> double xneg;
          >>>
          >>> cout << "This program will compute a quadratic equation\n";
          >>> cout << "Please enter a number: \n";
          >>> cin >> a;
          >>> cout << "Please enter another number: \n";
          >>> cin >> b;
          >>> cout << "Please enter a final number: \n";
          >>> cin >> c;
          >>>
          >>> square = quad(a,b,c);
          >>> xneg = neg(a,b,square) ;
          >>> xpos = plus(a,b,square );
          >>>
          >>> cout << "The positive function is: " << xpos << "\n";
          >>> cout << "The negative function is: " << xneg << "\n";
          >>>
          >>> return 0;
          >>> }[/color]
          >>
          >> (...)
          >>[/color][/color]
          (...)[color=blue]
          > seems unlikely to me that its a bug, as Comeau is giving following error
          > for that code
          >
          > "ComeauTest .c", line 27: error: "plus" is ambiguous
          > xpos = plus(a,b,square );
          >
          > Comeau could by buggy as well, but i wouldnt bet on it(, at last without
          > long session with standard and rules of lookup and function choosing)[/color]
          Yes, you are right, I would not bet myself. I have tried with Comeau,
          and you are right.

          So, I have looked at the conflicting part:

          <stl_function.h >

          template <class _Tp>
          struct plus : public binary_function <_Tp,_Tp,_Tp> {
          _Tp operator()(cons t _Tp& __x, const _Tp& __y) const { return __x +
          __y; }
          };

          So there is the problem: The compiler is unable to decide if we want:

          double plus(double,dou ble,double)

          or

          double plus<double>::o perator()(doubl e,double,double )

          In this case, it is not trying to decide between overloaded functions,
          but between two pretty different ways to interpret the statement. And we
          may suppose Comeau is right, as usual.

          Regards

          Comment

          • Jaspreet

            #6
            Re: function call misinterpreted as a variable

            sjbrown8@eng.us f.edu wrote:[color=blue]
            > I have the piece of code below, and when i try compiling with the line
            >
            > g++ 753075304.cpp
            >
            > I get the following error message:
            >
            > 753075304.cpp: In function 'int main()':
            > 753075304.cpp:2 9: error: 'plus' was not declared in this scope
            >
            > I've gone over the code several times line by lin, and my eyes are
            > starting to bleed. It looks totally fine to me, and I'm actually
            > wondering whether I found a compiler bug, but more likely some weird
            > feature, or I'm just totally missing something. And before anyone asks,
            > yes it is homework, but I am trying to grade it, not do it. my version
            > of GCC (as reported by its man page) is 4.0.1 2005-07-11 and I am
            > running debian testing, kernel version 2.6.8-1-386 #1
            >[/color]
            [snip program]

            I get the following error using g++. I guess its self explanatory:

            rt.cc: In function `int main()':
            rt.cc:27: use of `plus' is ambiguous
            rt.cc:5: first declared as `double plus(double, double, double)' here
            /usr/include/c++/3.2.2/bits/stl_function.h: 128: also declared as `
            template<class _Tp> struct std::plus' here
            rt.cc:27: use of `plus' is ambiguous
            rt.cc:5: first declared as `double plus(double, double, double)' here
            /usr/include/c++/3.2.2/bits/stl_function.h: 128: also declared as `
            template<class _Tp> struct std::plus' here

            There is a plus function in stl_function.h and one in your code. So,
            the compiler cannot determine which function needs to be called.

            Now thats the problem with using namespace std. If you remove the
            'using namespace std' and explicitly add std to calls to cout and cin,
            then your program would compile properly.

            I guess there's a point in C++ FAQs which focuses on why usage of
            namespace may cause a compile time error.

            Comment

            • Thomas Maeder

              #7
              Re: function call misinterpreted as a variable

              "Zorro" <zorabi@comcast .net> writes:
              [color=blue]
              > I compiled it in visual studio, and it had no errors.
              > Sorry, for not having better news. It is a GCC bug.[/color]

              Hardly. When gcc and a Microsoft compiler disagree, it's normally a
              bug in the latter. As this time.

              Comment

              • Thomas Maeder

                #8
                Re: function call misinterpreted as a variable

                sjbrown8@eng.us f.edu writes:
                [color=blue]
                > int main()
                > {
                > double xpos;
                > double a;
                > double b;
                > double c;
                > double square;
                > double xneg;
                >
                > cout << "This program will compute a quadratic equation\n";
                > cout << "Please enter a number: \n";
                > cin >> a;
                > cout << "Please enter another number: \n";
                > cin >> b;
                > cout << "Please enter a final number: \n";
                > cin >> c;
                >
                > square = quad(a,b,c);[/color]

                Side note:

                This is very unsafe code. If one of the input operations fails,
                reading its value causes the program to have undefined behavior. A
                program should always first check for success of an input operation
                before using the value (apparently) read.

                Comment

                • Zorro

                  #9
                  Re: function call misinterpreted as a variable

                  > Hardly. When gcc and a Microsoft compiler disagree, it's normally a[color=blue]
                  > bug in the latter. As this time.[/color]

                  I fully agree. However, 5 minutes after typing that message I was fast
                  asleep, so I made a sleepy judgment.

                  Regards,
                  Z.

                  Comment

                  • skaller

                    #10
                    Re: function call misinterpreted as a variable

                    On Mon, 03 Oct 2005 10:39:35 +0000, Zara wrote:
                    [color=blue]
                    >
                    > So, I have looked at the conflicting part:
                    >
                    > <stl_function.h >
                    >
                    > template <class _Tp>
                    > struct plus : public binary_function <_Tp,_Tp,_Tp> {
                    > _Tp operator()(cons t _Tp& __x, const _Tp& __y) const { return __x +
                    > __y; }
                    > };
                    >
                    > So there is the problem: The compiler is unable to decide if we want:
                    >
                    > double plus(double,dou ble,double)
                    >
                    > or
                    >
                    > double plus<double>::o perator()(doubl e,double,double )
                    >
                    > In this case, it is not trying to decide between overloaded functions,
                    > but between two pretty different ways to interpret the statement. And we
                    > may suppose Comeau is right, as usual.[/color]

                    IMHO:

                    What is actually happening is that the gcc compiler is seeing
                    the 'plus' and assuming it is a class name, ignoring the function.
                    Then it is looking for an operator conversion:

                    plus::operator double()const

                    The operator() is entirely irrelevant: in fact the signature would be

                    double plus<double>::o perator()const( double, double);

                    note: TWO arguments not three. In fact, if it we were just overloading
                    the relevant signatures would be

                    plus() // generator default ctor
                    plus(plus const&) // generator copy stor
                    plus(double,dou ble) // operator()
                    plus(double,dou ble,double) // user defined function

                    and all three are entirely disjoint, and can be overloaded
                    without any possible ambiguity.

                    So it has nothing at all to do with overloading, rather,
                    the compiler has to choose whether plus is a class name or
                    a function name, it is choosing a class for an unknown reason
                    BEFORE bothering to find the constructor. It is trying
                    in vain to find an operator conversion. It finds instead

                    double plus(double,dou ble,double);

                    which it should NOT find (I mean it shouldn't even see this
                    function) and thinks it is supposed to be a member of class plus,
                    but declared in the wrong scope (should have been in std along with
                    the class plus).

                    This is a bug in gcc, no question about it (FYI: I'm using 4.0 on Ubuntu)
                    at this point it is trying to do overload resolution on

                    operator double()
                    double(plus)

                    [it can find a constructor for class double with argument plus
                    too, except that double is a built in type ..]

                    double f(double);
                    struct f{};

                    int main() {
                    struct f x;
                    x = f(1.0);
                    }


                    abc.cpp:6: error: no match for ‘operator=’ in ‘x = f(1.0e+0)’
                    abc.cpp:2: note: candidates are: f& f::operator=(co nst f&)

                    Shows clearly that when a class and function are declared
                    in the same scope, the class takes precedence. And here:

                    struct f{};
                    double f(double);

                    int main() {
                    struct f x = f(1.0);
                    }

                    abc.cpp:5: error: conversion from ‘double’ to non-scalar type ‘f’ requested

                    it is clear again, the function f is just ignored.
                    These messages are both sensible (whether or not the algorithm
                    is correct). This message:

                    753075304.cpp:2 9: error: 'plus' was not declared in this scope

                    is plain garbage. Comeau's message is more sensible:

                    "ComeauTest .c", line 27: error: "plus" is ambiguous

                    meaning, it can't decide if plus is a function or class.
                    Note this isn't an overload ambiguity, but a kinding
                    ambiguity (is it a typename or a function name?)


                    --
                    John Skaller <skaller at users dot sf dot net>
                    Try Felix, the successor to C++ http://felix.sf.net


                    Comment

                    • M

                      #11
                      Re: function call misinterpreted as a variable


                      Ok, to compile this code in g++ remove the function definitions and move
                      the function implementation to the top of the file. Also, some other
                      modifications below:
                      [color=blue]
                      > #include <iostream>
                      > #include <cmath>[/color]

                      // After the header files...
                      using namespace std;

                      // No need for function definitions
                      [color=blue]
                      > double quad(double n1, double n2, double n3)
                      > {
                      > double y;
                      > double z;
                      > double w;
                      > double root;
                      >
                      > y = 4*n1*n3;
                      > z = n2 * n2;
                      > w = z-y;
                      > root = sqrt(w);
                      > return root;
                      > }
                      >
                      > double plus( double n1, double n2, double n3)
                      > {
                      > double x;
                      >
                      > x= (-1 * n2 + n3)/(2*n1);
                      > return x;
                      > }
                      >
                      > double neg(double n1, double n2, double n3)
                      > {
                      > double x;
                      >
                      > x= (-1 * n2 - n3)/(2*n1);
                      > return x;
                      > }[/color]
                      [color=blue]
                      > int main()
                      > {
                      > double xpos;
                      > double a;
                      > double b;
                      > double c;
                      > double square;
                      > double xneg;
                      >
                      > cout << "This program will compute a quadratic equation\n";
                      > cout << "Please enter a number: \n";
                      > cin >> a;
                      > cout << "Please enter another number: \n";
                      > cin >> b;
                      > cout << "Please enter a final number: \n";
                      > cin >> c;
                      >
                      > square = quad(a,b,c);
                      > xneg = neg(a,b,square) ;
                      > xpos = plus(a,b,square );
                      >
                      > cout << "The positive function is: " << xpos << "\n";
                      > cout << "The negative function is: " << xneg << "\n";
                      >
                      > return 0;
                      > }[/color]

                      Regards,

                      Michael

                      Comment

                      Working...