c++ - [bug?] intitialisation of array of structs
- Craig Bowlas (43/43) Jun 16 2006 Hi,
- Walter Bright (4/13) Jun 17 2006 Any struct that has a member that has a constructor, must itself have a
- Craig Bowlas (35/48) Jun 20 2006 Thanks for the clarification, I found this issue while trying to
- Walter Bright (3/58) Jun 20 2006 try:
- Craig Bowlas (8/66) Jun 21 2006 Thanks Walter,
Hi, The code .. #include "stdio.h" using namespace std; #include <string> struct stuff { string a; string b; }; void main (void) { stuff things[]= { {"bill","fred"}, {"one","two2"}, {"CD","DVD"} }; for (int i=0; i<3;i++) { printf("item %d %s\n", i, things[i].b.c_str()); } } produces the compiler errors {"bill","fred"}, ^ test.cpp(37) : Error: cannot find constructor for class matching stuff::stuff(char const *) {"one","two2"}, ^ test.cpp(38) : Error: cannot find constructor for class matching stuff::stuff(char const *) {"CD","DVD"} ^ test.cpp(39) : Error: cannot find constructor for class matching stuff::stuff(char const *) If the types in the struct are built-ins (tested with int and char *) and the initialisation array adjusted in the first case, the code compiles and works as I would expect. Is this a bug or am I being stupid? Regards Craig
Jun 16 2006
Craig Bowlas wrote:#include "stdio.h" using namespace std; #include <string> struct stuff { string a; string b; };Any struct that has a member that has a constructor, must itself have a constructor. std::string has a constructor. Therefore, stuff must have a constructor.
Jun 17 2006
On Sat, 17 Jun 2006 21:29:57 -0700, Walter Bright <newshound digitalmars.com> wrote:Craig Bowlas wrote:Thanks for the clarification, I found this issue while trying to rebuild a code::blocks nightly with dmc and so I went to plan B and attempted to use mingw/gcc for now. I have revisited the above and have added a constructor for the stuff struct, but I still get the same errors, the code now reads ... struct stuff { string a; string b; stuff(string c, string d); }; stuff::stuff(string c, string d) { a=c; b=d; }; void main (void) { stuff things[]= { {"john", "bill"}, {"2", "one"}, {"3", "CD"} }; for (int i=0; i<3;i++) { printf("item %d %s\n", i, things[i].b); } } _____ Craig Bowlas craig bowlas.demon.co.uk#include "stdio.h" using namespace std; #include <string> struct stuff { string a; string b; };Any struct that has a member that has a constructor, must itself have a constructor. std::string has a constructor. Therefore, stuff must have a constructor.
Jun 20 2006
Craig Bowlas wrote:On Sat, 17 Jun 2006 21:29:57 -0700, Walter Bright <newshound digitalmars.com> wrote:try: stuff("john", "bill"),Craig Bowlas wrote:Thanks for the clarification, I found this issue while trying to rebuild a code::blocks nightly with dmc and so I went to plan B and attempted to use mingw/gcc for now. I have revisited the above and have added a constructor for the stuff struct, but I still get the same errors, the code now reads ... struct stuff { string a; string b; stuff(string c, string d); }; stuff::stuff(string c, string d) { a=c; b=d; }; void main (void) { stuff things[]= { {"john", "bill"},#include "stdio.h" using namespace std; #include <string> struct stuff { string a; string b; };Any struct that has a member that has a constructor, must itself have a constructor. std::string has a constructor. Therefore, stuff must have a constructor.{"2", "one"}, {"3", "CD"} }; for (int i=0; i<3;i++) { printf("item %d %s\n", i, things[i].b); } } _____ Craig Bowlas craig bowlas.demon.co.uk
Jun 20 2006
On Tue, 20 Jun 2006 15:48:43 -0700, Walter Bright <newshound digitalmars.com> wrote:Craig Bowlas wrote:Thanks Walter, that does indeed compile and run as expected, thanks again Regards Craig _____ Craig Bowlas craig bowlas.demon.co.ukOn Sat, 17 Jun 2006 21:29:57 -0700, Walter Bright <newshound digitalmars.com> wrote:try: stuff("john", "bill"),Craig Bowlas wrote:Thanks for the clarification, I found this issue while trying to rebuild a code::blocks nightly with dmc and so I went to plan B and attempted to use mingw/gcc for now. I have revisited the above and have added a constructor for the stuff struct, but I still get the same errors, the code now reads ... struct stuff { string a; string b; stuff(string c, string d); }; stuff::stuff(string c, string d) { a=c; b=d; }; void main (void) { stuff things[]= { {"john", "bill"},#include "stdio.h" using namespace std; #include <string> struct stuff { string a; string b; };Any struct that has a member that has a constructor, must itself have a constructor. std::string has a constructor. Therefore, stuff must have a constructor.{"2", "one"}, {"3", "CD"} }; for (int i=0; i<3;i++) { printf("item %d %s\n", i, things[i].b); } } _____ Craig Bowlas craig bowlas.demon.co.uk
Jun 21 2006