2011-06-02 1 views
2

私は2つの異なるコンパイラで同じプロジェクトをコンパイルしています。スニペットは、以下VS2008の下で罰金コンパイルが、私は、Eclipseで++ G下G ++ではコンパイラエラーですが、VS2008ではなくstlテンプレート

G ++エラーメッセージ、それをコンパイルしようとすると、次のエラーを与える:

common.h: In function 'int ci_find_substr(const T&, const T&, const std::locale&)': 
common.h:84: error: expected `;' before 'it' 
common.h:86: error: 'it' was not declared in this scope 

ソースコードスニペット

#include <stdio.h> 
#include <string.h> 
#include <stdarg.h> 

#include <string> 
#include <algorithm> 
#include <locale> 
#include <iostream> 
#include <algorithm> 

using namespace std ; 

// templated version of my_equal so it could work with both char and wchar_t 
template<typename charT> 
struct my_equal { 
    my_equal(const std::locale& loc) : loc_(loc) {} 
    bool operator()(charT ch1, charT ch2) { 
     return std::toupper(ch1, loc_) == std::toupper(ch2, loc_); 
    } 
private: 
    const std::locale& loc_; 
}; 

// find substring (case insensitive) 
template<typename T> 
int ci_find_substr(const T& str1, const T& str2, const std::locale& loc = std::locale()) 
{ 
    T::const_iterator it = std::search(str1.begin(), str1.end(), 
     str2.begin(), str2.end(), my_equal<T::value_type>(loc)); 
    if (it != str1.end()) return it - str1.begin(); 
    else return -1; // not found 
} 

ヘルプ、提案、ヒントが役立ちます。おかげさまで

答えて

2

お試しくださいtypename T::const_iterator it = std::search(etc.)タイプとしてT::const_iteratorを認識しているとは思わないと思います。私はG ++ 4.5.2で、あなたのコードをコンパイルしようとしたとき

、それは私の編集この

error: need 'typename' before 'T:: const_iterator' because 'T' is a dependent scope 

を確認し、次のエラーメッセージを与えた:あなたはまた、my_equal<T::value_type>(loc)にそれを必要とします。 my_equal<typename T::value_type>(loc)に変更する

+0

私はあなたの提案のいくつかのバリエーションを試して、次のエラーが発生しました。 [エラー:テンプレートパラメータの型メンバーを参照するには、 'typename T :: value_type'を使用してください。 –

+0

typename T :: const_iterator it = std :: search(str1.begin()、str1.end –

+0

@Steven私の編集を参照 – jonsca