Topic: Confused compiler or confused programmer.
Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/06/06 Raw View
Peter Lawrey wrote:
>
> I have a program for which the errors are contradictory or incorrect.
> I am using Sun's CC compiler.
>
> class test {
> int value;
> public:
> test(int value) {
> this->value = value;
> }
>
> int value() {
> return value;
> }
>
> int twice_value() {
> return 2 * value();
> }
> };
>
> Returns the errors;
> "test.cpp", line 5: Error: The name value is ambiguous, test::value and
> test::value().
> "test.cpp", line 8: Warning: value hides test::value.
> "test.cpp", line 9: Error: The name value is ambiguous, test::value and
> test::value().
> "test.cpp", line 13: Error: The name value is ambiguous, test::value and
> test::value().
> "test.cpp", line 13: Error: Only a function may be called.
> 4 Error(s) and 1 Warning(s) detected.
>
> It appears that the compiler must know if an experssion is a function of
> a variable before it sees how it is being used.
No, the result of name lookup doesn't depend on whether the result
would be valid in the context. (The exceptions are names following
the keywords "class" or "template", in which case only names in
the appropriate category are considered.)
After a name has been looked up, its validity is determined.
Answering the question in the subject, the programmer is confused. :-)
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Jack J. Woehr" <jwoehr@ibm.net>
Date: 2000/06/06 Raw View
Peter Lawrey wrote:
> I have a program for which the errors are contradictory or incorrect.
> I am using Sun's CC compiler.
>
> class test {
> int value;
> public:
> test(int value) {
> this->value = value;
> }
>
> int value() {
> return value;
You can't have a variable with the same name as a function. I, too, thought
this was silly when I first learned C++, seems like obvious scope for the
priniciple of 'overloading'.
--
Jack J. Woehr # Ceterum censeo
PO Box 51, Golden, CO 80402 # in herbas belli
http://www.well.com/~jax/rcfb # ab idem desistamus.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Peter Lawrey <wdlawp@mbox.com.au>
Date: 2000/06/05 Raw View
I have a program for which the errors are contradictory or incorrect.
I am using Sun's CC compiler.
class test {
int value;
public:
test(int value) {
this->value = value;
}
int value() {
return value;
}
int twice_value() {
return 2 * value();
}
};
Returns the errors;
"test.cpp", line 5: Error: The name value is ambiguous, test::value and
test::value().
"test.cpp", line 8: Warning: value hides test::value.
"test.cpp", line 9: Error: The name value is ambiguous, test::value and
test::value().
"test.cpp", line 13: Error: The name value is ambiguous, test::value and
test::value().
"test.cpp", line 13: Error: Only a function may be called.
4 Error(s) and 1 Warning(s) detected.
It appears that the compiler must know is an experssion is a function of
a variable before it sees how it is being used.
In the first error, the only legal interpritation if that value is a
variable. You cannot re-assign a declared function.
The second error could be considered ambiguous, but short of renaming
the variable with ugly _, is there something else I can do?
The third error, first states that it is ambiguous and then states that
it can only be a function !?
Peter Lawrey.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 2000/06/06 Raw View
In article <393AC77B.617A73DF@mbox.com.au>,
Peter Lawrey <wdlawp@mbox.com.au> wrote:
> I have a program for which the errors are contradictory or incorrect.
> I am using Sun's CC compiler.
>
> class test {
> int value;
> public:
> test(int value) {
> this->value = value;
> }
>
> int value() {
> return value;
> }
>
> int twice_value() {
> return 2 * value();
> }
> };
The code is ill-formed.
"Only function declarations can be overloaded; object and type
declarations cannot be overloaded." (clause 13).
Unfortunately, the standard imposes no requirement on the
intelligibility of diagnostic messages ;-)
--
Jim
I ignore all email from recruitment agencies. Except that
I reserve the right to send rude, nasty replies to recruiters.
Please do not send me email with questions - post
here.
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Peter Hallowes" <phallowes@berty.com>
Date: 2000/06/06 Raw View
A member method name cannot be the same as a member variable name.
That leaves you with a warning on line 8, which you can eradicate by either
using m_value to denote that the variable is a member variable, or using
#pragma on whichever warning category it belongs to.
Peter Lawrey <wdlawp@mbox.com.au> wrote in message
news:393AC77B.617A73DF@mbox.com.au...
> I have a program for which the errors are contradictory or incorrect.
> I am using Sun's CC compiler.
>
> class test {
> int value;
> public:
> test(int value) {
> this->value = value;
> }
>
> int value() {
> return value;
> }
>
> int twice_value() {
> return 2 * value();
> }
> };
>
> Returns the errors;
> "test.cpp", line 5: Error: The name value is ambiguous, test::value and
> test::value().
> "test.cpp", line 8: Warning: value hides test::value.
> "test.cpp", line 9: Error: The name value is ambiguous, test::value and
> test::value().
> "test.cpp", line 13: Error: The name value is ambiguous, test::value and
> test::value().
> "test.cpp", line 13: Error: Only a function may be called.
> 4 Error(s) and 1 Warning(s) detected.
>
> It appears that the compiler must know is an experssion is a function of
> a variable before it sees how it is being used.
> In the first error, the only legal interpritation if that value is a
> variable. You cannot re-assign a declared function.
> The second error could be considered ambiguous, but short of renaming
> the variable with ugly _, is there something else I can do?
> The third error, first states that it is ambiguous and then states that
> it can only be a function !?
>
> Peter Lawrey.
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]