同样的老故事 - VS vs GCC 4.6.1

时间:2011-11-05 14:55:53

标签: c++ visual-studio-2010 gcc4

下面的代码可以很好地编译VS2010,但无法使用gcc 4.6.1进行编译:

来自gcc的错误:

* C:... \ Calculator_engine_impl.h | 20 |错误:无法调用'(std :: string {aka std :: basic_string})(_ _ gn_cxx :: __ normal_iterator>&amp ;, __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

1 个答案:

答案 0 :(得分: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*&)来满足此调用,但失败了。显然,视觉工作室做了不同的事情。我不完全确定哪个符合规范 - 但我怀疑海湾合作委员会是在这里做到的。

当然,解决方案是不要对局部变量和函数使用相同的名称。