1

Using VS2010 and C++, I am using a supplier library to interface to their USB industrial camera.

The library has an base abstract class for data stream sinks called GrabberSinkType, and one of derived classes from that is MediaStreamSink which deals with writing streams to video files. One of the methods supported by MediaStreamSink (not a virtual function of the base class) is getFilename() which simply returns the name of the file being written to.

The main Grabber class which handles streaming has a method getSinkTypePtr(), which returns a pointer to the sink in current use, defined as smart_ptr<GrabberSinkType> getSinkTypePtr() const;.

What I need to do is check if is a MediaStreamSink, and if so, get the filename. And I cannot get it to compile. Having checked if it is a media stream type, all I am doing is

CString    SinkPath = dynamic_cast<tMediaStreamSinkPtr>(m_cGrabber.getSinkTypePtr())->getFilename().c_str();

(tMediaStreamSinkPtr is typedef'd assmart_ptr<MediaStreamSink>). This results in error 2680:

"error C2680: 'DShowLib::tMediaStreamSinkPtr' : invalid target type for dynamic_cast target type must be a pointer or reference to a defined class"

I have tried every possible combination of dynamic casts, static casts, old style casts I can think of, and whilst the error changes, I cannot get it to compile.

I clearly don't know what I'm doing here, how do I get a pointer to the dervied class and successfully call its getFilename() method?

nmw01223
  • 1,611
  • 3
  • 23
  • 40
  • 3
    template instantiations of two types are not related to each other even if the types are related. Whatever `smart_ptr` is needs to provide a specialised dynamic cast like [`std::shared_ptr` does](https://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast) – Alan Birtles Feb 27 '22 at 14:08
  • 2
    And in fact that library provides [`static_smart_ptr_cast`](https://www.theimagingsource.com/support/documentation/ic-imaging-control-cpp/meth_descDShowLib_static_smart_ptr_cast.htm) – Igor Tandetnik Feb 27 '22 at 18:00
  • Excellent, it even has a relevant example, thank you both very much. – nmw01223 Feb 27 '22 at 18:51

0 Answers0