473,569 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
}
Oct 3 '05 #1
10 2066
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.us f.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:2 9: 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.us f.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:2 9: 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.us f.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:2 9: 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()(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
Oct 3 '05 #5
sj******@eng.us f.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: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

[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.us f.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()(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.


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
Oct 4 '05 #10

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

Similar topics

9
4940
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or...
3
14916
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
8
4821
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
7666
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 like mine to run immediately after it. So in the code below, what JS would i need to add to my "myfile.inc" page so that I could guarantee this...
35
2428
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 time in considering this post, and for your comments. I wrote this function to simplify the task of combining strings using mixed sources. I often...
2
12435
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 undefined. I am trying to make a rollover effect using codebehind. I have a <a> tag that onmouseover will call a sub in codebehind to change a...
28
4294
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
2110
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
3000
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. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables....
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.