2011-11-05 47 views
-2

下面的代碼編譯罰款與VS2010但沒有用gcc 4.6.1編譯:同樣的老故事 - VS VS GCC 4.6.1

從GCC錯誤:

* C:\ Calculator_engine_impl.h | 20 |錯誤:不對應的呼叫 '(的std :: string {又名性病:: 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

什麼行導致錯誤? – BlackBear

+3

我的源代碼中沒有提到對'QString'或'QChar'的任何引用。你確定你發佈了正確的位? –

+0

@BlackBear會導致錯誤的行:result - = term_(beg,end); 這是從計算fnc。 – smallB

回答

2

您有一個名爲term_的函數和一個局部變量:

Int_T Calculator_engine<Int_T>::term_(Forward_Iterator& beg,Forward_Iterator& end) 
// .... 

QString term_ = read_next_token_(beg,end); 
// ... 
result -= term_(beg,end); 

GCC使用最內層的定義 - 在這種情況下,您的本地QString。然後它嘗試找到operator()(QChar*&, QChar*&)以滿足此調用,但失敗。顯然,視覺工作室做了一些不同的事情。我不完全確定哪個符合規範 - 但我懷疑GCC是否在這裏得到它。

當然,解決方案是不要爲局部變量和函數使用相同的名稱。

+0

謝謝,是的,gcc很可能是正確的。因爲我已經開始使用gcc,所以我對VS正在編譯的錯誤數量感到不知所措,而gcc正在拒絕它們。好東西。 – smallB