2011-11-05 4 views
-2

Le code ci-dessous compile très bien avec VS2010, mais ne parvient pas à compiler avec gcc 4.6.1:même histoire - VS vs GCC 4.6.1

L'erreur de gcc:

* C: ... \ Calculator_engine_impl.h | 20 | erreur: pas de match pour appel à '(std :: string {alias std :: basic_string}) (__gnu_cxx :: __ normal_iterator> &, __gnu_cxx :: __ normal_iterator> &)' | *

#include "stdafx.h" 


#include <iostream> 
#include "Calculator_engine.h" 

int main(int argc, char** argv) 
{ 
    QString expression("1+2-3"); 
    auto beg = expression.begin(); 
    auto end = expression.end(); 
    while (beg != end) 
    { 
    qDebut() << 
    Calculator_engine<>::read_next_token_(beg,end); 
    } 
} 

#ifndef CALCULATOR_ENGINE_H 
#define CALCULATOR_ENGINE_H 
#include <string> 
#include <cctype> 
using namespace std; 
//#include "Incorrect_Expression.h" 
template<class Int_T = long long> 
class Calculator_engine 
{ 
private: 
    Calculator_engine(); 
    static Int_T expression(QString exp); 
    template<class Forward_Iterator> 
    static Int_T term_(Forward_Iterator& beg,Forward_Iterator& end); 
public: 

    template<class Forward_Iterator> 
    static QString read_next_token_(Forward_Iterator& beg,Forward_Iterator& end); 

public: 

    static QString calculate(QString exp); 
}; 

#include "Calculator_engine_impl.h" 

#endif // CALCULATOR_ENGINE_H 
#ifndef CALCULATOR_ENGINE_IMPL_H_INCLUDED 
#define CALCULATOR_ENGINE_IMPL_H_INCLUDED 
template<class Int_T> 
class Calculator_engine;//[Forward decl] 

template<class Int_T> 
template<class Forward_Iterator> 
Int_T Calculator_engine<Int_T>::term_(Forward_Iterator& beg,Forward_Iterator& end) 
{ 
    QChar token; 
    Int_T result; 
    switch(token) 
    { 
    case '*': 
     break; 
    case '/': 
     break; 
    } 
} 
template<class Int_T> 
QString Calculator_engine<Int_T>::calculate(QString exp) 
{ 
    Int_T result; 
    auto beg = exp.begin(); 
    auto end = exp.end(); 
    while (beg != end) 
    { 
     QString term_ = read_next_token_(beg,end); 
     QChar token = read_next_token_(beg,end); 
    switch(token) 
    { 
    case '-': 
     result -= term_(beg,end); 
     break; 
    case '+': 
     result += term_(beg,end); 
     break; 
    } 


    } 

} 

template<class Int_T> 
Int_T Calculator_engine<Int_T>::expression(QString exp) 
{ 

} 


template<class Int_T> 
template<class Forward_Iterator> 
    QString Calculator_engine<Int_T>::read_next_token_(Forward_Iterator& beg,Forward_Iterator& end) 
    { 
     QString result; 
     while(std::isdigit(*beg)) 
     { 

     } 
     return result; 
    } 

#endif // CALCULATOR_ENGINE_IMPL_H_INCLUDED 
+3

Quelle ligne provoque l'erreur? – BlackBear

+3

Je ne vois aucune référence à 'QString' ou' QChar' dans votre code source. Etes-vous sûr que vous avez posté les bons morceaux? –

+0

Ligne @BlackBear qui provoque l'erreur: result - = term_ (beg, end); cela provient de calculer fnc. – smallB

Répondre

2

Vous avez à la fois une fonction nommée term_ et une variable locale:GCC utilise la définition la plus interne - dans ce cas, votre QString local. Il essaie ensuite de trouver un operator()(QChar*&, QChar*&) pour satisfaire cet appel, mais échoue. Apparemment, le studio visuel fait quelque chose de différent. Je ne suis pas tout à fait sûr de ce qui est conforme à la spécification - mais je soupçonne que GCC est juste ici.

La solution, bien sûr, est de ne pas utiliser le même nom pour une variable locale et une fonction.

+0

merci, et oui, gcc a probablement raison. Depuis que j'ai commencé à utiliser gcc, je suis submergé par le nombre d'erreurs que compilait VS et gcc les refuse. Bonne chose. – smallB