Topic: ...[C]an't take address of register, bit field, constant or string
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/10/14 Raw View
Anthony DeRobertis wrote:
>
> Is the following legal?
>
> struct Window { /* ... */ };
> typedef Window* WindowPtr;
> struct Point { /* ... */ };
> extern short FindWindow(Point p, WindowPtr *x);
>
> void func(Point p) {
> FindWindow(p,&Window());
> }
>
> It seems to me that it is...I think one can take the address of a
> temportary...but MrCpp (compiler) gives the error message "can't take
> address of register, bit field, constant or string."
The type of "Window()" is "Window". The type of "&Window()" is
"Window*". The type of the second parameter to FindWindow is
"WindowPtr*", or "Window**". You have a type mismatch. It looks to me
like the compiler is giving you the wrong error message. If you declare
the second parameter as WindowPtr, or Window*, then it should work.
--
Ciao,
Paul
[ 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/10/15 Raw View
Anthony DeRobertis wrote:
>
> Is the following legal?
>
> struct Window { /* ... */ };
> typedef Window* WindowPtr;
> struct Point { /* ... */ };
> extern short FindWindow(Point p, WindowPtr *x);
>
> void func(Point p) {
> FindWindow(p,&Window());
> }
>
> It seems to me that it is...I think one can take the address of a
> temportary...but MrCpp (compiler) gives the error message "can't take
> address of register, bit field, constant or string."
The operand of operator& must be an lvalue. A temporary is an rvalue.
Therefore you cannot take the address of a temporary.
However, if you really want Window rvalues to be addressable, you
can simply add
Window* operator&() { return this; }
to the Window definition.
BTW, your code shouldn't compile anyway, since the type of &Window()
would be Window*, while your function takes WindowPtr*, which is
Window**.
(Except, of course, if your Window struct contains a conversion
function to Window* ...)
---
[ 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 ]