2011-09-23 55 views
1

此问题可能是我误解Objective-C(++)的局限性以及它如何与C++交互的直接结果。或者可能是我错过了在我面前的东西。iOS - GCC无法看到似乎清晰可见的函数

我有一个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 
+0

你有'编译器'(第二个arg)编译错误,而似乎无法在源中找到它 – fazo

+0

std :: vector的std :: allocator是默认初始化,所以你可以忽略AFAIK! – Goz

+0

请问您可以粘贴Image.h的任何相关部分。 – Perception

回答

3

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

你要拨打的免费版本,所以你需要说:

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

无关提示:当everythign更好地把所有的东西变成自己的名称空间,否则,你可能迟早会遇到相关困难是在全局命名空间

+0

是这是问题是什么。非常感谢!!我似乎没有回想过这个问题。也许只是Visual Studio比XCode的GCC版本产生更好的错误描述:D – Goz