[solved] Type conversion problem: TechDraw::BaseGeom* versus TechDraw::BaseGeomPtr

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
edi
Posts: 483
Joined: Fri Jan 17, 2020 1:32 pm

[solved] Type conversion problem: TechDraw::BaseGeom* versus TechDraw::BaseGeomPtr

Post by edi »

Hi everybody,
I have the two functions:

Code: Select all

void BaseGeom::intersectionLL(TechDraw::BaseGeomPtr geom1,  TechDraw::BaseGeomPtr geom2, std::vector<Base::Vector3d>& interPoints)
{
	...
}


std::vector<Base::Vector3d> BaseGeom::intersection(TechDraw::BaseGeomPtr geom2)
{
	...
	std::vector<Base::Vector3d> interPoints;
	intersectionLL(this, geom2, interPoints); // <-- compiler message
}

...
TechDraw::BaseGeomPtr geom1 = ...
TechDraw::BaseGeomPtr geom2 = ...
interPoints = geom1->intersection(geom2);
The compiler returns the message:
error: cannot convert ‘TechDraw::BaseGeom*’ to ‘TechDraw::BaseGeomPtr’ {aka ‘std::shared_ptr<TechDraw::BaseGeom>’}
The compiler message occures, because "this" is of type TechDraw::BaseGeom*, it should be TechDraw::BaseGeomPtr.
How can I convert "this" ?
Last edited by edi on Fri Jan 28, 2022 11:02 am, edited 1 time in total.
wmayer
Founder
Posts: 20320
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Type conversion problem: TechDraw::BaseGeom* versus TechDraw::BaseGeomPtr

Post by wmayer »

You have to make BaseGeom a subclass of std::enable_shared_from_this. Then inside your function you can use shared_from_this().

Currently BaseGeom is declared in Geometry.h with

Code: Select all

class TechDrawExport BaseGeom
...
and you have to replace this line with

Code: Select all

class TechDrawExport BaseGeom : public std::enable_shared_from_this<BaseGeom>
...
edi
Posts: 483
Joined: Fri Jan 17, 2020 1:32 pm

Re: Type conversion problem: TechDraw::BaseGeom* versus TechDraw::BaseGeomPtr

Post by edi »

Thank you for the very quick answer. It was the solution.
Post Reply