Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 12, 2014 struct template help | ||||
---|---|---|---|---|
| ||||
Please consider the following struct arc(T,U) { T some_var; U someother_var; } /* things */ class myclass { this(){} ~this(){} void MYfunction() { arc!(string, string[]) * a; a.some_var = "hello"; } } void main() { c = new myclass(); c.MYfunction(); } This leads to a segmentation fault. What am I doing wrong? |
July 12, 2014 Re: struct template help | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | Also, (*c).MYfunction() is leading to segmentation fault |
July 12, 2014 Re: struct template help | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | sorry, I meant (*a).some_var not (*c).MYfunction() |
July 12, 2014 Re: struct template help | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | On Saturday, 12 July 2014 at 19:09:44 UTC, seany wrote:
> Please consider the following
>
> struct arc(T,U)
> {
>
> T some_var;
> U someother_var;
> }
>
> /* things */
>
> class myclass
> {
> this(){}
> ~this(){}
> void MYfunction()
>
> {
> arc!(string, string[]) * a;
> a.some_var = "hello";
> }
> }
>
>
> void main()
> {
> c = new myclass();
> c.MYfunction();
> }
>
>
> This leads to a segmentation fault.
>
> What am I doing wrong?
"a" has not been instantiated. You are declaring it as a pointer to struct and using its fields without initializing it. "arc!(string, string[]) a;" will work.
|
July 12, 2014 Re: struct template help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Danyal Zia | On Saturday, 12 July 2014 at 19:16:52 UTC, Danyal Zia wrote:
> On Saturday, 12 July 2014 at 19:09:44 UTC, seany wrote:
>> Please consider the following
>>
>> struct arc(T,U)
>> {
>>
>> T some_var;
>> U someother_var;
>> }
>>
>> /* things */
>>
>> class myclass
>> {
>> this(){}
>> ~this(){}
>> void MYfunction()
>>
>> {
>> arc!(string, string[]) * a;
>> a.some_var = "hello";
>> }
>> }
>>
>>
>> void main()
>> {
>> c = new myclass();
>> c.MYfunction();
>> }
>>
>>
>> This leads to a segmentation fault.
>>
>> What am I doing wrong?
> "a" has not been instantiated. You are declaring it as a pointer to struct and using its fields without initializing it. "arc!(string, string[]) a;" will work.
For reasons further down in the software, I need to do this with a pointer. How do I do it with a pointer, please?
|
July 12, 2014 Re: struct template help | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | On Saturday, 12 July 2014 at 19:19:28 UTC, seany wrote:
> For reasons further down in the software, I need to do this with a pointer. How do I do it with a pointer, please?
I don't know what are you trying to achieve, but if that's what you want, you can do:
void MYfunction()
{
auto strArr = ["Hello", "World!"];
arc!(string, string[]) * a = new arc!(string, string[])("s", strArr);
a.some_var = "hello";
}
If you want to use "a" from outside the function you have to declare it in the class and then initialize it in your function.
|
July 12, 2014 Re: struct template help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Danyal Zia | do I have to initialize all variables of the struct? or may I also use a this(){} in the struct and initialize only those which are known at a given moment? |
July 12, 2014 Re: struct template help | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | On 07/12/2014 12:19 PM, seany wrote: > On Saturday, 12 July 2014 at 19:16:52 UTC, Danyal Zia wrote: >> On Saturday, 12 July 2014 at 19:09:44 UTC, seany wrote: >>> arc!(string, string[]) * a; >>> a.some_var = "hello"; >> "a" has not been instantiated. You are declaring it as a pointer to >> struct and using its fields without initializing it. "arc!(string, >> string[]) a;" will work. > > For reasons further down in the software, I need to do this with a > pointer. How do I do it with a pointer, please? By making that pointer point to an actual object. :) arc!(string, string[]) * a = new arc!(string, string[])(); Shorter syntax: auto a = new arc!(string, string[]); You can assign it later as well: arc!(string, string[]) * a; // ... a = new arc!(string, string[])(); Preferably, don't assign to the member but construct when the information is available: auto a = new arc!(string, string[])("hello", ["merhaba", "hola", "bonjour"]); Ali |
July 12, 2014 Re: struct template help | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | On 07/12/2014 12:32 PM, seany wrote: > do I have to initialize all variables of the struct? No. The uninitialized ones get their .init values. > or may I also use a > this(){} in the struct and initialize only those which are known at a > given moment? That already works with structs. You don't need to define any constructor if the type is simple data i.e. if you don't want to set any invariants, just construct with whatever is available at that moment: auto a = new S("hello"); However, if it is important that someother_var should not remain as an empty array, then you better define a constructor and demand both values when constructing. Ali |
July 12, 2014 Re: struct template help | ||||
---|---|---|---|---|
| ||||
Posted in reply to seany | On Saturday, 12 July 2014 at 19:32:48 UTC, seany wrote:
> do I have to initialize all variables of the struct? or may I also use a this(){} in the struct and initialize only those which are known at a given moment?
You can initialize in constructor this(), but you can't initialize partial fields of struct when using pointer to struct. There would be other trivial ways to do what you are trying to do. Also, I never used any pointer to struct (which is of value type) in my code .
|
Copyright © 1999-2021 by the D Language Foundation