al@watsup.waterloo.edu (Al Vermeulen) (05/11/89)
Could someone please explain to me what is wrong with this code? It seems innocent enough. Does it compile with cfront 2.0? To get around this problem I've been passing references to functions like func and then creating a local variable in func (gross, eh?). code: struct A { A::A( A& ) {} }; void func( A ) { } void func2() { A a; func(a); } cfront 1.2.1 says: "weird.cc", line 13: error: argument 1 of type A & expected for A::A() 1 error -- Al Vermeulen (al@watsup, ahvermen@watcgl) al@watsup.waterloo.edu
jss@hector.UUCP (Jerry Schwarz) (05/11/89)
In article <9640@watcgl.waterloo.edu> al@watsup.waterloo.edu (Al Vermeulen) asks for help in understanding why some code fails to compile. I have simplified his example slightly struct A { A( A& ) ; }; void func2() { A a; // This declaration is rejected } C++ normally "synthesizes" two constructors for a class A. The one with no arguments and the one with an A& argument. When you declare any constructor C++ assumes that you want to specify all the constructors and therefore does not synthesize any. The declaration of a requires such a constructor with no arguments. To solve the problem you need to add a definition of the constructor with no arguments. struct A { A(A&) ; A() { } } ; Jerry Schwarz AT&T Bell Labs, Murray Hill
ark@alice.UUCP (Andrew Koenig) (05/11/89)
In article <9640@watcgl.waterloo.edu>, al@watsup.waterloo.edu (Al Vermeulen) writes: > Could someone please explain to me what is wrong with this code? It seems code: struct A { A::A( A& ) {} }; When you declare a class with a constructor, the compiler automatically removes the void constructor that you otherwise get by default. That is, because you defined A::A(A&), you must also explicitly define A::A() if you want it. void func( A ) { } void func2() { A a; You have declared an A object without an initializer. This tried to call A::A(), which doesn't exist. Hence it's an error. func(a); } -- --Andrew Koenig ark@europa.att.com
dan@oresoft.uu.net (Daniel Elbaum) (05/12/89)
In article <9640@watcgl.waterloo.edu> al@watsup.waterloo.edu (Al Vermeulen) writes: Could someone please explain to me what is wrong with this code? It seems innocent enough. Does it compile with cfront 2.0? To get around this problem I've been passing references to functions like func and then creating a local variable in func (gross, eh?). code: struct A { A::A( A& ) {} // Use A( A& ) {} instead }; void func( A ) { } void func2() { A a; // this constructor call needs an argument func(a); } Since you've defined a constructor for A, any definition of an instantiation of an A will cause it to be called. Since the defined constructor takes an argument, Cfront is rightly asking for one to go with the declaration of a. Func2() should look like this: void func2() { A a(a); func(a); } -- Not responsible : uunet - for this posting : tektronix \!oresoft!dan : reed / dan@oresoft.uu.net : sun!nosun -