2011-06-02 76 views
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'] – 2011-06-02 18:00:21

+0

typename T :: const_iterator it = std :: search(str1.begin(),str1.end(), – 2011-06-02 18:01:42

+0

@Steven看到我的编辑 – jonsca 2011-06-02 18:13:20