digitalmars.D.learn - Typedef of class doesn't work?
- Bradley Smith <digitalmars-com baysmith.com> Jan 14 2007
- Charles D Hixson <charleshixsn earthlink.net> Feb 03 2007
import std.stdio;
class A {
this(float v) { value = v; }
float getValue() { return value; }
private:
float value;
}
typedef A B;
void main() {
B b = new B(1);
writefln(b.value);
}
The above code produces the following errors:
dmd testTypedef.d
B(1F)) of type testTypedef.A to B
testTypedef.d(15): Error: this for value needs to be type A not type B
Aliasing works for classes, but not typedef. Why not?
Thanks,
Bradley
Jan 14 2007
Bradley Smith wrote:import std.stdio; class A { this(float v) { value = v; } float getValue() { return value; } private: float value; } typedef A B; void main() { B b = new B(1); writefln(b.value); } The above code produces the following errors: >dmd testTypedef.d testTypedef.d(14): Error: cannot implicitly convert expression (new B(1F)) of type testTypedef.A to B testTypedef.d(15): Error: this for value needs to be type A not type B Aliasing works for classes, but not typedef. Why not? Thanks, Bradley
(Seems, not is.) I don't have a small example but I defined typedef uint NodeNbr; typedef NodeNbr ObjAddr; and then got: charles mandala1:~/projects/D/Parody$ dmd -c db.d db.d(37): constructor node.Node.this (NodeFile,NodeNbr) does not match parameter types (NodeFile,uint) db.d(37): Error: cannot implicitly convert expression (n) of type uint to NodeNbr charles mandala1:~/projects/D/Parody$ dmd -c db.d db.d(37): constructor node.Node.this (NodeFile,NodeNbr) does not match parameter types (NodeFile,NodeNbr) db.d(37): Error: cannot implicitly convert expression (n) of type NodeNbr to NodeNbr charles mandala1:~/projects/D/Parody$ dmd -c db.d db.d(37): constructor node.Node.this (NodeFile,NodeNbr) does not match parameter types (NodeFile,NodeNbr) db.d(37): Error: cannot implicitly convert expression (nd) of type NodeNbr to NodeNbr (Each separate compilation represents a different way of arranging things.) FWIW, they were calling a class defined in another file, and I got around this by using: typedef node.NodeNbr ObjAddr; This does make sense. But the error message is so totally unhelpful that this almost doesn't seem worth the effort. When I used alias instead of typedef it compiled without error. That was the first step in figuring out what the problem was. Better error messages are REALLY needed!
Feb 03 2007








Charles D Hixson <charleshixsn earthlink.net>