开发者

iOS - GCC failing to see a function that seems to be plainly visible

开发者 https://www.devze.com 2023-04-07 17:09 出处:网络
This problem may be as a direct result of either me misunderstanding the limitations of Objective-C(++) and how it interacts with C++.Or it may be a case of me missing something thats right in front o

This problem may be as a direct result of either me misunderstanding the limitations of Objective-C(++) and how it interacts with C++. Or it may be a case of me missing something thats right in front of me.

I have a C++ class that does some Objective-C++ work behind the scenes. As such I have a templated C++ class that 开发者_JS百科makes a call to a templated function. I have then specialised the templated function to create me a UIImage.

However when I build I get the following error.

../ImageProcessing/ImageProcessing/MacOS/MacOSImage.h:43: error: no matching function for call to 'MacOSImage<R5G5B5A1>::MakeUIImage(std::vector<R5G5B5A1, std::allocator<R5G5B5A1> >&, unsigned int, unsigned int)'
../ImageProcessing/ImageProcessing/MacOS/MacOSImage.h:41: note: candidates are: UIImage* MacOSImage<ColourSpace>::MakeUIImage() [with ColourSpace = R5G5B5A1]

My header file looks like this:

#ifndef ImageProcessing_MacOSImage_h
#define ImageProcessing_MacOSImage_h

#include "Image.h"

#ifdef __OBJC__
    #import <UIKit/UIImage.h>
#else
    typedef void UIImage;
#endif

template< typename ColourSpace > UIImage* MakeUIImage( const std::vector< ColourSpace >& colours, unsigned int width, unsigned int height )
{
    return NULL;
}

template< typename ColourSpace > class MacOSImage : public BaseImage< ColourSpace >
{
protected:

public:
    MacOSImage( unsigned int width, unsigned int height );
    virtual ~MacOSImage()   {};

    UIImage* MakeUIImage();
};

template< typename ColourSpace > inline MacOSImage< ColourSpace >::MacOSImage( unsigned int width, unsigned int height ) :
    BaseImage< ColourSpace >( width, height )
{
}

template< typename ColourSpace > inline UIImage* MacOSImage< ColourSpace >::MakeUIImage()
{
    return MakeUIImage( BaseImage< ColourSpace >::Pixels(), BaseImage< ColourSpace >::GetWidth(), BaseImage< ColourSpace >::GetHeight() );
}

#endif

I find this error rather confusing. Not least of all because if you ignore my specialisations there is a template function that ought to exactly match the prototype it is after at the top of the file. Its only disadvantage is that it will return NULL. However this would still give me an idea as to what is going on if I could get it to compile.

So has anyone got any ideas why this won't compile?

Cheers!

Edit: As requested here is Image.h:

#ifndef ImageProcessing_Image_h
#define ImageProcessing_Image_h

#include <vector>

template< typename ColourSpace > class BaseImage
{
protected:
    unsigned int                mWidth;
    unsigned int                mHeight;
    std::vector< ColourSpace >  mPixels;
public:
    BaseImage( unsigned int width, unsigned int height );
    virtual ~BaseImage()        {};

    std::vector< ColourSpace >& Pixels();
    const std::vector< ColourSpace >& Pixels() const;

    unsigned int GetWidth() const               { return mWidth;    }
    unsigned int GetHeight() const              { return mHeight;   }
    unsigned int GetBytesPerPixel() const       { return sizeof( ColourSpace );     }
    unsigned int GetBitsPerPixel() const        { return GetBytesPerPixel() * 8;    }
};

template< typename ColourSpace > inline BaseImage< ColourSpace >::BaseImage( unsigned int width, unsigned int height )  :
    mWidth( width ),
    mHeight( height ),
    mPixels( width * height )
{

}

template< typename ColourSpace > inline std::vector< ColourSpace >& BaseImage< ColourSpace >::Pixels()
{
    return mPixels;
}

template< typename ColourSpace > inline const std::vector< ColourSpace >& BaseImage< ColourSpace >::Pixels() const
{
    return mPixels;
}

#if     defined( _OSX )

#include "MacOS/MacOSImage.h"
#define Image MacOSImage

#elif   defined( _WIN32 )

#include "Win32/Win32Image.h"
typedef Win32Image Image;

#else

#error We need a platform specific implementation of the image class

#endif

#endif


This is classical name lookup problem. The compiler stops looking for names when it finds a function with the same name, ignoring the signature. Only then it checks the signature and finds it does not match.

You want to call the free version, so you need to say:

return ::MakeUIImage( BaseImage< ColourSpace >::Pixels(), 
BaseImage< ColourSpace >::GetWidth(), BaseImage< ColourSpace >::GetHeight() );

Unrelated hint: Better put all your stuff into your own namespace, otherwise you might sooner or later run into related difficulties when everythign is in the global namespace

0

精彩评论

暂无评论...
验证码 换一张
取 消