Thread overview
[bug?] intitialisation of array of structs
Jun 16, 2006
Craig Bowlas
Jun 18, 2006
Walter Bright
Jun 20, 2006
Craig Bowlas
Jun 20, 2006
Walter Bright
Jun 21, 2006
Craig Bowlas
June 16, 2006
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
June 18, 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.
June 20, 2006
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
June 20, 2006
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
June 21, 2006
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