473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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: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;
}
Oct 3 '05 #1
10 2053
I compiled it in visual studio, and it had no errors.
Sorry, for not having better news. It is a GCC bug.

Regards,
zo****@ZHMicro.com
http://www.zhmicro.com
http://distributed-software.blogspot.com
http://groups-beta.google.com/group/...Z?lnk=la&hl=en

Oct 3 '05 #2
sj******@eng.usf.edu wrote:
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
Oct 3 '05 #3
Zara wrote:
sj******@eng.usf.edu wrote:
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:


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)

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

Oct 3 '05 #4
Kyle wrote:
Zara wrote:
sj******@eng.usf.edu wrote:
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;
}


(...)

(...) 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>::operator()(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
Oct 3 '05 #5
sj******@eng.usf.edu wrote:
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.

Oct 3 '05 #6
"Zorro" <zo****@comcast.net> writes:
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.
Oct 3 '05 #7
sj******@eng.usf.edu writes:
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.
Oct 3 '05 #8
> 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.

Oct 3 '05 #9
On Mon, 03 Oct 2005 10:39:35 +0000, Zara wrote:

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

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

double plus<double>::operator()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::operator=(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?)
--
John Skaller <skaller at users dot sf dot net>
Try Felix, the successor to C++ http://felix.sf.net
Oct 4 '05 #10
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
Dec 30 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
35
by: michael.casey | last post by:
The purpose of this post is to obtain the communities opinion of the usefulness, efficiency, and most importantly the correctness of this small piece of code. I thank everyone in advance for your...
2
by: exshige | last post by:
I read from some book that you can actually on any tag in the html page put a onClick or something and assign it with the sub/function name that is in the codebehind. I tried but it say name...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.