iOS - GCC 无法看到似乎清晰可见的函数

发布于 2024-12-06 13:51:05 字数 3563 浏览 0 评论 0原文

这个问题可能是我误解 Objective-C(++) 的局限性以及它与 C++ 交互方式的直接结果。或者这可能是我错过了就在我面前的一些东西。

我有一个 C++ 类,它在幕后执行一些 Objective-C++ 工作。因此,我有一个模板化 C++ 类,它调用模板化函数。然后,我专门使用模板化函数来创建 UIImage。

但是,当我构建时,出现以下错误。

../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]

我的头文件如下所示:

#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

我发现这个错误相当令人困惑。尤其重要的是,因为如果您忽略我的专业化,那么有一个模板函数应该与文件顶部的原型完全匹配。它唯一的缺点是它将返回 NULL。然而,如果我能编译它,这仍然会让我知道发生了什么。

那么有人知道为什么这不能编译吗?

干杯!

编辑:根据要求,这里是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 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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌若浮生 2024-12-13 13:51:05

这是经典的名称查找问题。当编译器找到具有相同名称的函数时,它会停止查找名称,并忽略签名。只有这样它才会检查签名并发现它不匹配。

你想调用免费版本,所以你需要说:

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

不相关的提示:最好把你所有的东西都放到你自己的命名空间中,否则当所有东西都在全局命名空间中时,你迟早会遇到相关的困难

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文