Steven Smethurst
Steven Smethurst

Reputation: 4614

Compiler error in G++ but not in VS2008, stl templates

I am compiling the same project under two different compilers. The snippet below compiles fine under VS2008 but gives the following error when I try to compile it under G++ with Eclipse

G++ Error message:

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

Source code snippet

#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
}

Any help, suggestions, or hints would help. Thanks.

Upvotes: 2

Views: 821

Answers (1)

jonsca
jonsca

Reputation: 10381

Try putting typename T::const_iterator it = std::search(etc.) I don't think it's recognizing T::const_iterator as a type.

When I tried to compile your code in g++ 4.5.2, it gave me the following error message, which confirms this

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

Edit: You also need it on the my_equal<T::value_type>(loc). Should be changed to my_equal<typename T::value_type>(loc)

Upvotes: 2

Related Questions