【问题标题】:Cannot compile code with gcc 4.6.1无法使用 gcc 4.6.1 编译代码
【发布时间】:2011-10-27 15:45:40
【问题描述】:

下面的这段代码可以用 VS2010 编译,但不想用 gcc 4.6.1 编译:

#ifndef IS_CHAR_H_INCLUDED
#define IS_CHAR_H_INCLUDED
#include <type_traits>

template<class Int_T>
struct Is_Char_
{
    enum {value = false};
};

template<>
struct Is_Char_<char>
{
    enum {value = true};
};

template<>
struct Is_Char_<unsigned char>
{
    enum {value = true};
};

template<>
struct Is_Char_<signed char>
{
    enum {value = true};
};

template<class Int_T>
struct Is_Char : Is_Char_<typename std::remove_cv<Int_T>::type>
{

};

#endif // IS_CHAR_H_INCLUDED

#ifndef PROMOTE_H_INCLUDED
#define PROMOTE_H_INCLUDED
#include <type_traits>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/find.hpp>
#include <boost/mpl/next.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/mpl/end.hpp>
   //#include "Is_Char.h" doesn't have to be here this file is pasted above


/*Promotes Integer type to one up in size range*/
template<class Integer>
struct Promote
{
    static_assert(std::is_integral<Integer>::value,"Non Integer type is not allowed.");
    /*Check correct type - depending on Integer being signed or unsigned*/
    typedef typename std::conditional<std::is_signed<Integer>::value,
                                boost::mpl::vector<signed char,short,int,long,long long>,
    boost::mpl::vector<unsigned char,unsigned short,unsigned int,long,long long>
                                     >::type types;
    /*
    Find this type from the list above - substituting Integer for signed or unsigned char iff Integer is of type char
    */
    typedef typename boost::mpl::find<types,
    typename std::conditional<Is_Char<Integer>::value,
    typename std::conditional<std::is_signed<Integer>::value,signed char,unsigned char>::type, Integer>::type>::type this_type;

    /*If Integer is int and if size of it is == to long promote int to long long (iterate to next element twice)*/
    typedef typename boost::mpl::eval_if<boost::mpl::bool_<((std::is_same<Integer,int>::value || std::is_same<Integer,unsigned int>::value)
                                                                && (sizeof(int) == sizeof(long)))>,
                                         boost::mpl::next<typename boost::mpl::next<this_type>::type>,
                                         boost::mpl::next<this_type>
                                        >::type next_type;
    /*Check if iterator points within range or if one pass end which means that Integer was u/long long*/
    typedef typename std::conditional<std::is_same<typename boost::mpl::end<types>::type,next_type>::value,Integer,typename boost::mpl::deref<next_type>::type>::type type;
};

#endif // PROMOTE_H_INCLUDED

【问题讨论】:

  • 请添加错误信息。
  • 您确定您没有使用一些隐藏错误消息的构建实用程序或生成文件吗?
  • -1 是因为您没有提出问题,也没有表明您已经为解决问题付出了任何努力。显示您如何尝试编译它。显示一些错误消息(从第一条开始)。显示代码中第一个错误发生的位置。
  • 如果您没有看到错误消息,则说明您还不够努力。它们存在于某处。如果必须,请在命令行上运行编译器。但首先,请查看“构建消息”选项卡。
  • 由于您设置了-Wfatal-errors,因此列出的所有内容都有错误。

标签: c++ gcc boost


【解决方案1】:

我的猜测是您在编译时没有指定--std=c++0x,因此std::is_integral&lt;&gt; 等C++11 功能不可用。当我使用该选项时,您的代码会为我编译。

更新:现在您已经显示了编译器输出,问题是您已经启用了几乎所有可能的警告,并且还设置了-Wpedantic-errors 以将其中一些视为错误。其中许多警告是由完全合理的代码触发的,大多数作者(包括 Boost)不会花时间修复或解决所有这些警告。

您当然应该删除-Wpedantic-errors,除非您有特殊要求,即任何代码都不应使用特定于编译器的扩展;在这种情况下,您可能无法使用 Boost。禁用一些不太有用的警告也可能是一个好主意——你无法修复 Boost 生成的警告,所以它们所做的只是让你更难发现关于你的代码的真正警告。我通常的目标是使用-Wall -Wextra 进行干净编译。

【讨论】:

  • 是的。我的命令是g++-4.6.1 -c test.cpp --std=c++0x,它编译没有错误。
  • 不指定 boost 目录?
  • 它们在我的默认包含路径中。
  • 当我尝试使用您所显示的内容进行编译时,我从 gcc 告诉无法识别的选项 -4.6.1 得到错误
  • 我安装了几个不同的版本,名称不同。试试g++ -c test.cpp --std=c++0x
【解决方案2】:

真正的问题不在于您的代码。问题是你找不到编译器的错误信息。在你做任何其他事情之前解决这个问题!

【讨论】:

  • 有 59 个错误。所以某处有59条错误消息。你必须找出在哪里。
  • 我告诉你,没有列出!理解我在告诉你的内容有多难?!
【解决方案3】:

在终端中运行确切的g++ 命令。然后你会看到错误。不要为此使用 IDE。

【讨论】:

    猜你喜欢
    • 2013-10-09
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    相关资源
    最近更新 更多