C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem mit struct-Konstruktor

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
Johannes Totz
Guest





PostPosted: Fri Sep 10, 2004 12:44 am    Post subject: Problem mit struct-Konstruktor Reply with quote



Hi!

Kann mir jemand erklären, warum folgender Code nicht kompiliert?

class V
{
};

class C
{
public:
struct S1
{
V wert;
S1(V w) : wert(w) { }
};
struct S2
{
S1 wert;
S2(S1 w) : wert(w) { }
};
void func()
{
S1 b(V());
S2 a(b); // <-- MARKER
}
};

Kommentiere ich die markierte Zeile aus, schluckt gcc es.
Aber bleibt die Zeile drin, spuckt er folgende Fehlermeldung aus:

t2.cpp: In member function `void C::func()':
t2.cpp:25: no matching function for call to `C::S2::S2(C::S1 (&)(V (*)()))'
t2.cpp:17: candidates are: C::S2::S2(const C::S2&)
t2.cpp:19: C::S2::S2(C::S1)
t2.cpp:25: warning: unused variable `C::S2 a'

Ich hab mir jetzt total den Knoten ins Gehirn gegrübelt und bekomme
nicht raus, warum das nicht funzen soll.


MfG
Johannes

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Heinz Ozwirk
Guest





PostPosted: Fri Sep 10, 2004 7:17 am    Post subject: Re: Problem mit struct-Konstruktor Reply with quote



"Johannes Totz" <jo_t (AT) gmx (DOT) net> schrieb im Newsbeitrag news:chqtds$m0a$1 (AT) fuerst (DOT) cs.uni-magdeburg.de...
Quote:
Hi!

Kann mir jemand erklären, warum folgender Code nicht kompiliert?

class V
{
};

class C
{
public:
struct S1
{
V wert;
S1(V w) : wert(w) { }
};
struct S2
{
S1 wert;
S2(S1 w) : wert(w) { }
};
void func()
{
S1 b(V());

Auch wenn's für einen normalen Menschen nicht so aussieht - für den Compiler ist das die Deklaration einer Funktion. Wenn man statt dessen

S1 b = V();

schreibt, sollte es funktionieren.

Quote:
S2 a(b); // <-- MARKER
}
};

Kommentiere ich die markierte Zeile aus, schluckt gcc es.
Aber bleibt die Zeile drin, spuckt er folgende Fehlermeldung aus:

HTH
Heinz

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Werner Salomon
Guest





PostPosted: Fri Sep 10, 2004 7:22 am    Post subject: Re: Problem mit struct-Konstruktor Reply with quote



Johannes Totz <jo_t (AT) gmx (DOT) net> wrote

Quote:
Kann mir jemand erklären, warum folgender Code nicht kompiliert?

class V
{
};

class C
{
public:
struct S1
{
V wert;
S1(V w) : wert(w) { }
};
struct S2
{
S1 wert;
S2(S1 w) : wert(w) { }
};
void func()
{
S1 b(V());
S2 a(b); // <-- MARKER
}
};

Kommentiere ich die markierte Zeile aus, schluckt gcc es.
Aber bleibt die Zeile drin, spuckt er folgende Fehlermeldung aus:

t2.cpp: In member function `void C::func()':
t2.cpp:25: no matching function for call to `C::S2::S2(C::S1 (&)(V (*)()))'
t2.cpp:17: candidates are: C::S2::S2(const C::S2&)
t2.cpp:19: C::S2::S2(C::S1)
... 'b' scheint für den Compiler nicht vom Typ 'S1' zu sein.
t2.cpp:25: warning: unused variable `C::S2 a'

Hallo Johannes,
ich vermute, dass das Problem in der Zeile vorher steckt. Versuche
mal:
void func()
{
V v;
S1 b(v);
S2 a(b); // <-- MARKER
}

evt. hält der Compiler den Ausdruck 'S1 b(V());' für den Prototyp
einer Funktion vom Typ 'C::S1 (*)( V )'.

Gruß
Werner

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Christoph Rabel
Guest





PostPosted: Fri Sep 10, 2004 7:22 am    Post subject: Re: Problem mit struct-Konstruktor Reply with quote

Johannes Totz wrote:
Quote:

void func()
{
S1 b(V());

Das Problem liegt in dieser Zeile, nicht erst in der nächsten.

Das ist leider eine Funktionsdeklaration, kein Objekt. Hier
schlagen ein paar ziemlich schräge Parsingregeln zu. Die
meisten Compiler schlucken es mit zusätzlichen Klammer:

S1 b((V()));

Quote:
Ich hab mir jetzt total den Knoten ins Gehirn gegrübelt und bekomme
nicht raus, warum das nicht funzen soll.

Deine structs und die markierte Zeile sind völlig in Ordnung.

mfg

Christoph

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Markus Moll
Guest





PostPosted: Fri Sep 10, 2004 7:26 am    Post subject: Re: Problem mit struct-Konstruktor Reply with quote

Hallo

Johannes Totz wrote:

Quote:
Kann mir jemand erklären, warum folgender Code nicht kompiliert?

Ja.

Quote:
void func()
{
S1 b(V());
S2 a(b); // <-- MARKER
}

t2.cpp: In member function `void C::func()':
t2.cpp:25: no matching function for call to `C::S2::S2(C::S1 (&)(V
(*)()))' t2.cpp:17: candidates are: C::S2::S2(const C::S2&)
t2.cpp:19: C::S2::S2(C::S1)
t2.cpp:25: warning: unused variable `C::S2 a'

Das ist ein bißchen verwirrend, liegt aber in der C++ Grammatik begraben.
Die Grammatik ist hier insofern mehrdeutig, als daß
S1 b(V())
wie von dir beabsichtigt, die Deklaration eines S1-Objekts b mit V()
initialisiert bedeuten könnte, oder aber auch eine Deklaration einer
Funktion mit Rückgabewert S2 und (unbenanntem) Parameter "Zeiger auf
Funktion mit Rückgabewert V und ohne Parameter".
Die Konfliktauflösung bevorzugt letzteres. Möchtest du klarmachen, daß du
gerne ein S1 Objekt erstellen würdest, kannst du stattdessen z.B.
S1 b = V();
schreiben.

Gruß
Markus

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Marcel Müller
Guest





PostPosted: Fri Sep 10, 2004 7:33 am    Post subject: Re: Problem mit struct-Konstruktor Reply with quote

Johannes Totz wrote:

Quote:
void func()
{
S1 b(V());

Da gibt es wohl eine Mehrdeutigkeit zwischen der gewünschten Bedeutung
und einer Funktion b mit dem Returnwert S1 und als Parameter einem
Pointer auf eine parameterlose Funktion mit dem Rückgabewert V.

Eigentlich würde ich erwarten für die gerade beschriebene Beduetung
müßte man schreiben
S1 b(V(*)());

Ich vermute daher einen *Compilerfehler*.

Folgende Alternativen funktionieren:

S1 b((V)V());
oder
S1 b = V();


Quote:
S2 a(b); // <-- MARKER

Das war dann ein Folgefehler.

Quote:
}
};


Marcel

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Fri Sep 10, 2004 1:04 pm    Post subject: Re: Problem mit struct-Konstruktor Reply with quote

Johannes Totz <jo_t (AT) gmx (DOT) net> wrote


Quote:
Kann mir jemand erklären, warum folgender Code nicht kompiliert?

class V
{
};

class C
{
public:
struct S1
{
V wert;
S1(V w) : wert(w) { }
};
struct S2
{
S1 wert;
S2(S1 w) : wert(w) { }
};
void func()
{
S1 b(V());

Hier declarierst Du eine Funktion. Bist Du sicher, dass das ist was Du
willst?

Wenn Du hier eine Variable declarieren willst, musst Du schreiben:
S1 b( (V()) ) ;
oder
S1 b = S1( V() ) ;
oder
auto S1 b( V() ) ;
Immerhin irgendwas, damit die Deklaration keine Funktion sein kann.

Quote:
S2 a(b); // <-- MARKER

Une hier versuchst Du, eine Variable Types S2 mit der Adresse der
Funktion zu initialisieren. Es gibt aber keinen passenden Constructor.

Quote:
}
};

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Johannes Totz
Guest





PostPosted: Fri Sep 10, 2004 1:36 pm    Post subject: Re: Problem mit struct-Konstruktor Reply with quote

Danke an alle für die Tipps!
Jetzt funzt es.


MfG
Johannes

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Olaf Krzikalla
Guest





PostPosted: Fri Sep 10, 2004 3:35 pm    Post subject: Re: Problem mit struct-Konstruktor Reply with quote

Hi,

[email]kanze (AT) gabi-soft (DOT) fr[/email] schrieb:
Quote:
auto S1 b( V() ) ;
Steht denn in irgendeinem DR, dass das jetzt schon funktionieren soll?

AFAIK war es früher nicht so und es wurde ja auch vorgeschlagen, dass
auto in zukünftigen Versionen neben der decl-Semantik auch obige Aufgabe
(Unterscheidung Deklaration -Variablendefinition) bekommt.

MfG
Olaf Krzikalla

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Mon Sep 13, 2004 2:42 pm    Post subject: Re: Problem mit struct-Konstruktor Reply with quote

Olaf Krzikalla <Entwicklung (AT) reico (DOT) de> wrote


Quote:
kanze (AT) gabi-soft (DOT) fr schrieb:
auto S1 b( V() ) ;
Steht denn in irgendeinem DR, dass das jetzt schon funktionieren soll?
AFAIK war es früher nicht so

Warum soll es nicht funktionnieren? (Jetzt, dass Du es sagst, denke ich,
dass ich auch irgendwo gelesen habe, dass es nicht funktionniert. Ich
kann nicht aber daran erinnern, warum.)

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Olaf Krzikalla
Guest





PostPosted: Tue Sep 14, 2004 10:02 am    Post subject: Re: Problem mit struct-Konstruktor Reply with quote

Hi,

Quote:
Warum soll es nicht funktionnieren?
Nach dem *warum* mußt Du natürlich andere Leute fragen Wink

Die Diskussion fand in comp.std.c++ statt und ist schon ein paar Jahre
her. Der Thread begann mit Message-ID
<052201c0b4ab$603e8050$0500a8c0 (AT) dragonsys (DOT) com>


MfG
Olaf Krzikalla

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed Sep 15, 2004 9:21 am    Post subject: Re: Problem mit struct-Konstruktor Reply with quote

Olaf Krzikalla <Entwicklung (AT) reico (DOT) de> wrote


Quote:
Warum soll es nicht funktionnieren?

Nach dem *warum* mußt Du natürlich andere Leute fragen Wink
Die Diskussion fand in comp.std.c++ statt und ist schon ein paar Jahre
her. Der Thread begann mit Message-ID
052201c0b4ab$603e8050$0500a8c0 (AT) dragonsys (DOT) com

Leider in diesem Thread wurde es auch nur gesagt, dass es illegal ist,
ohne Erklärung warum.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.