function call misinterpreted as a variable

S

sjbrown8

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:29: 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;
}
 
Z

Zara

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:29: error: 'plus' was not declared in this scope
(...)

#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;
}
(...)

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
 
K

Kyle

Zara said:
(...)

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:

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)
 
Z

Zara

Kyle said:
(...)
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)
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()(const _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,double,double)

or

double plus<double>::eek:perator()(double,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
 
J

Jaspreet

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:29: 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
[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.
 
T

Thomas Maeder

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

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

Thomas Maeder

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

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.
 
Z

Zorro

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

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

Regards,
Z.
 
S

skaller

So, I have looked at the conflicting part:

<stl_function.h>

template <class _Tp>
struct plus : public binary_function<_Tp,_Tp,_Tp> {
_Tp operator()(const _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,double,double)

or

double plus<double>::eek:perator()(double,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.

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::eek:perator double()const

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

double plus<double>::eek: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,double) // operator()
plus(double,double,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,double,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::eek:perator=(const 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:29: 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?)
 
M

M

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:
#include <iostream>
#include <cmath>

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

// No need for function definitions
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;
}
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;
}

Regards,

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top