同样的老故事 - VS vs GCC 4.6.1

发布于 2024-12-13 20:35:43 字数 2640 浏览 1 评论 0原文

下面的代码可以在 VS2010 中正常编译,但在 gcc 4.6.1 中无法编译:

来自 gcc 的错误:

*C:...\Calculator_engine_impl.h|20|error: 与调用 '(std::string {aka 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

The code below compiles fine with VS2010 but fails to compile with gcc 4.6.1:

The error from gcc:

*C:...\Calculator_engine_impl.h|20|error: no match for call to '(std::string {aka 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2024-12-20 20:35:43

您有一个名为 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 在这里是正确的。

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

You have both a function named term_ and a local variable:

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 uses the innermost definition - in this case, your local QString. It then tries to find an operator()(QChar*&, QChar*&) to satisfy this call, but fails. Apparently visual studio does something different. I'm not entirely sure which is conforming to the spec - but I'd suspect GCC is getting it right here.

The solution, of course, is to not use the same name for a local variable and a function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文