Can not connvert int to AcDbObjectIdArray error when NULL is assign to AcDbObjectIdArray variable in ObjectARX 2023.

Can not connvert int to AcDbObjectIdArray error when NULL is assign to AcDbObjectIdArray variable in ObjectARX 2023.

nkokate052
Contributor Contributor
1,228 Views
8 Replies
Message 1 of 9

Can not connvert int to AcDbObjectIdArray error when NULL is assign to AcDbObjectIdArray variable in ObjectARX 2023.

nkokate052
Contributor
Contributor

Please resolve this issue.

If I assign NULL to AcDbObjectIdArray variable in ObjectARX 2023 in visual studio 2019 it is giving error can not convert from int to AcDbObjectIdArray. Screen shot is attached with this.

 

Example : 

AcDbObjectIdArray arrObj = NULL;

 

this giving error can not convert from int to AcDbObjectIdArray.

0 Likes
Accepted solutions (3)
1,229 Views
8 Replies
Replies (8)
Message 2 of 9

tbrammer
Advisor
Advisor

Just write  AcDbObjectIdArray arrObj;   to get an empty array.


Thomas Brammer ● Software Developer ● imos AGLinkedIn
If an answer solves your problem please [ACCEPT SOLUTION]. Otherwise explain why not.

Message 3 of 9

nkokate052
Contributor
Contributor

but i have to set default value to AcDbObjectIdArray variable in function declaration.

What can I do to set default value?

0 Likes
Message 4 of 9

tbrammer
Advisor
Advisor

AcDbObjectIdArray has a very nice default constructor that creates an empty array of AcDbObjectIds. You just don't need a default value. If you insist in assigning a default value (i.e. for syntactical reasons) you can do something like this:

AcDbObjectIdArray emptyIdArray; // global variable

void fkt() {
    AcDbObjectIdArray ids1 = emptyIdArray;
    AcDbObjectIdArray ids2 = AcDbObjectIdArray();
}

 

AcDbObjectIdArray is a typedef: 

    typedef AcArray<AcDbObjectId> AcDbObjectIdArray;

The assignment operator of AcArray<T,R> only accepts another AcArray:

    AcArray<T,R>&         operator =  (const AcArray<T,R>&);
    AcArray<T,R>&         operator =  (AcArray<T,R>&&);

 

 


Thomas Brammer ● Software Developer ● imos AGLinkedIn
If an answer solves your problem please [ACCEPT SOLUTION]. Otherwise explain why not.

Message 5 of 9

autodaug
Autodesk
Autodesk
Accepted solution

@nkokate052 wrote:

but i have to set default value to AcDbObjectIdArray variable in function declaration.

What can I do to set default value?


It sounds like you might be passing the array by value, as in:  myfunc(AcDbObjectIdArray idArr)

That's not usually the best practice, because it can cause unnecessary copying of the array during func calls.

If the array is not being modified, you can pass a const ref, as in:  myfunc(const AcDbObjectIdArray & idArr)

And if it *is* being modified, then pass it as a non-const ref: myfunc(AcDbObjectIdArray & idArr)

Or, if the array arg is optional, you can make it a ptr :  myfunc(const AcDbObjectIdArray *pArr = nullptr);

 

Message 6 of 9

daniel_cadext
Advisor
Advisor

looks like you're trying to do an optional argument, you can pass by pointer.

    static void Foo()
    {
        AcDbObjectIdArray ids;
        func(10, &ids);
        acutPrintf(_T("\nfoo %ld"), ids.length());
    }

    static void Far()
    {
        AcDbObjectIdArray ids;
        func(10);
        acutPrintf(_T("\nfar = %ld"), ids.length());
    }

    static void foobar()
    {
        std::unique_ptr<AcDbObjectIdArray> pids; //is null
        func(10, pids.get());
        if (pids)
            acutPrintf(_T("\nfoobar = %ld"), pids->length());
        else
            acutPrintf(_T("\nfoobar = %ld"), 0);
    }

    static void furbar()
    {
        std::unique_ptr<AcDbObjectIdArray> pids(new AcDbObjectIdArray());
        func(10, pids.get());
        if (pids)
            acutPrintf(_T("\nfurbar = %ld"), pids->length());
        else
            acutPrintf(_T("\nfurbar = %ld"), 0);

    }

    static void func(size_t cnt, AcDbObjectIdArray* ids = nullptr)
    {
        if (ids != nullptr)
        {
            for (size_t idx = 0; idx < cnt; idx++)
                ids->append(AcDbObjectId::kNull);
        }
    }

 

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes
Message 7 of 9

tbrammer
Advisor
Advisor
Accepted solution

As I suggested before you can pass a default value to a function like this: 

 

int defArr(AcDbObjectIdArray arr = AcDbObjectIdArray());

 

 or like this:

 

//Header file:
extern AcDbObjectIdArray defaultIdArray; // global variable
int defArr2(AcDbObjectIdArray arr=defaultIdArray);


//CPP file:
AcDbObjectIdArray defaultIdArray;

int defArr2(AcDbObjectIdArray arr) {
    return arr.length(); //i.e.
}

 

@autodaug's approach is a little more efficient.

But if you don't want to change function signatures you can do it like this.


Thomas Brammer ● Software Developer ● imos AGLinkedIn
If an answer solves your problem please [ACCEPT SOLUTION]. Otherwise explain why not.

0 Likes
Message 8 of 9

autodaug
Autodesk
Autodesk
Accepted solution

I agree, if you want to pass the array by value, and have a default parameter, then it should be = AcDbObjectIdArray()

The second approach with the global defaultIdArray object is more complicated and less flexible, imo.

 

 

 

0 Likes
Message 9 of 9

daniel_cadext
Advisor
Advisor

I assumed it was an [in][out] parameter, with a global or a new there's no way to know since there's no null.

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes