2014-02-15 47 views
1

我有以下代碼:預期類型說明符錯誤GCC

在文件 「的defs.h」

namespace ABCD 
{ 
template < typename T > 
class TPixelSum_TraitFor 
{ 
    public: 
     typedef double AccumType; 
}; 


template <> 
class TPixelSum_TraitFor<MonochromeImage::PixelType> 
{ 
    public: 
     typedef /*unsigned*/ long AccumType; 
}; 
} 

,並在文件中 「GraphicLibrary.h」

#include "Defs.h" 

using namespace ABCD; 
using namespace std; 

template < typename T, typename ACC_TRAIT = TPixelSum_TraitFor<T> > 
class SumImage : public TImageProcessor<T> 
{ 
    public: 

     typedef typename ACC_TRAIT::AccumType AccumType; 

    private: 

     AccumType fSum; 
}; 

和我收到以下錯誤

'T之前的預期類型說明符PixelSum_TraitFor」

預期 '>' 前 'TPixelSum_TraitFor'

在線路

模板<類型名稱T,類型名ACC_TRAIT = TPixelSum_TraitFor < T>>

的代碼是用g ++編譯4.8.1

回答

1

此代碼在MSVC++ 11.0 U4下編譯時無錯誤。唯一的問題有人抱怨是未定義的類型指定爲基類:

TImageProcessor<T> 

你確定這種類型在此文件的範圍內已知的?我不熟悉GCC錯誤信息,但是這個語法:

template < typename T, typename ACC_TRAIT = TPixelSum_TraitFor<T> > 

是完全有效的,所以他可能會抱怨下一行。

更新:

我用g ++ 4.8.1測試了你的代碼。 我分裂內容兩個文件並刪除未知類型:

test.h

namespace ABCD 
{ 
    template < typename T > 
    class TPixelSum_TraitFor 
    { 
    public: 
    typedef double AccumType; 
    }; 

    template <> 
    class TPixelSum_TraitFor< long /*MonochromeImage::PixelType*/ > //MonochromeImage is unknown 
    { 
    public: 
    typedef long AccumType; 
    }; 
} 

test.cpp

#include "test.h" 

using namespace ABCD; 
using namespace std; 

template < typename T, typename ACC_TRAIT = TPixelSum_TraitFor<T> > 
class SumImage //: public TImageProcessor<T> -> TImageProcessor is also unknown 
{ 
public: 
    typedef typename ACC_TRAIT::AccumType AccumType; 

private: 
    AccumType fSum; 
}; 

int main() 
{ 
} 

命令: g++ -o test.o test.cpp

結果:OK,沒有錯誤。

+0

Upvoted的答案,我也無法重現此錯誤。 – Ali

+0

這可能是由於未知類型的註釋。但我只是在猜測,GCC是我不太熟悉的東西。 –

+0

那麼,我們不應該猜測,他應該發佈一個[SSCCE](http://sscce.org/),以便我們可以診斷問題。我也投票結束這個問題。 – Ali