Discussion:
[Swig-user] C++ structure - no appropriate default constructor available
schullq
2017-05-05 14:52:42 UTC
Permalink
I have this nested strucuture:

class Test
{
public:
struct NestStruct
{
NestStruct(int st)
{
assert(st == READ || st == WRITE || st == DISABLED);
value_ = st;
}

enum
{
READ,
WRITE,
DISABLED
};

operator bool() const
{
return value_ != DISABLED;
}

operator int() const
{
return value_;
}

private:
int value_;

};
[...]
};

And somewhere in the lib I try to wrap, i have this class with this
constructor:

class MyClass
{
public:
MyClass(RandomClass *cmd, Test::NestedStruct type);
[...]
};

The problem is that in the C wrapper code, this happens:

SWIGEXPORT void * SWIGSTDCALL CSharp_MyClass__SWIG_0___(void * jarg1, void *
jarg2) {
void * jresult ;
RandomClassB *arg1 = (RandomClassB *) 0 ;
* Test::NestedStruct arg2 ;*
Test::NestedStruct *argp2 ;
MyClass *result = 0 ;
[...]
}

Swig tries to instanciate the struct, but there is no default ctor, and I
don't know how to handle it...
Any help ?



--
View this message in context: http://swig.10945.n7.nabble.com/C-structure-no-appropriate-default-constructor-available-tp15154.html
Sent from the swig-user mailing list archive at Nabble.com.
schullq
2017-05-09 11:52:10 UTC
Permalink
Always on the same problem for a couple of days now, I think there is no
solution to this.

The entire C wrapped code looks like this:

SWIGEXPORT void * SWIGSTDCALL
CSharp_LibLogicalAccessfCard_new_MyClass__SWIG_0___(void * jarg1, void *
jarg2) {
void * jresult ;
RandomClassB *arg1 = (RandomClassB *) 0 ;
Test::NestedStruct arg2 ;
Test::NestedStruct *argp2 ;
MyClass *result = 0 ;

arg1 = (RandomClassB *)jarg1;
argp2 = (Test::NestedStruct *)jarg2;
if (!argp2) {
SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException,
"Attempt to dereference null Test::NestedStruct", 0);
return 0;
}
arg2 = *argp2;
result = (MyClass *)new MyClass(arg1,arg2);
jresult = (void *)result;
return jresult;
}

Actually, SWIG is trying to instantiate arg2, but it is useless, since it
just return the jarg2 casts in (Test::NestedStruct *).

What is should do is erase this line:

Test::NestedStruct arg2 ;

And replace those lines that way:

result = (MyClass *)new MyClass(arg1,arg2);
[..]
arg2 = *argp2;

by

result = (MyClass *)new MyClass(arg1,*argp2);

Now, my problem is: how can I write this code manually ?



--
View this message in context: http://swig.10945.n7.nabble.com/C-structure-no-appropriate-default-constructor-available-tp15154p15155.html
Sent from the swig-user mailing list archive at Nabble.com.
schullq
2017-05-09 13:43:32 UTC
Permalink
Well, shame on me.

It has finally worked. It seems that I just included the file in the wrong
order...

My bad, swig handles it perfectly actually.



--
View this message in context: http://swig.10945.n7.nabble.com/C-structure-no-appropriate-default-constructor-available-tp15154p15156.html
Sent from the swig-user mailing list archive at Nabble.com.

Loading...