www.digitalmars.com         C & C++   DMDScript  

c++ - [bug?] intitialisation of array of structs

reply Craig Bowlas <craigbowlas yahoo.co.uk> writes:
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
parent reply Walter Bright <newshound digitalmars.com> writes:
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
parent reply Craig Bowlas <craig bowlas.demon.co.uk> writes:
On Sat, 17 Jun 2006 21:29:57 -0700, Walter Bright
<newshound digitalmars.com> wrote:

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.
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
Jun 20 2006
parent reply Walter Bright <newshound digitalmars.com> writes:
Craig Bowlas wrote:
 On Sat, 17 Jun 2006 21:29:57 -0700, Walter Bright
 <newshound digitalmars.com> wrote:
 
 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.
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"},
try: stuff("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
Jun 20 2006
parent Craig Bowlas <craig bowlas.demon.co.uk> writes:
On Tue, 20 Jun 2006 15:48:43 -0700, Walter Bright
<newshound digitalmars.com> wrote:

Craig Bowlas wrote:
 On Sat, 17 Jun 2006 21:29:57 -0700, Walter Bright
 <newshound digitalmars.com> wrote:
 
 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.
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"},
try: stuff("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
Thanks Walter, that does indeed compile and run as expected, thanks again Regards Craig _____ Craig Bowlas craig bowlas.demon.co.uk
Jun 21 2006