c++ - Visual Studio 2015 C++项目错误C++ 11标准

标签 c++ c++11 visual-studio-2015 compiler-errors

我在编译项目时遇到问题。我使用Visual Studio 2015,出现错误的代码是:

template <typename AtomicElement>
class Granule {
public:

typedef spare::Sequence<AtomicElement> RepresentantType;

unsigned int            SymbolID;    // identifier of the symbol
unsigned int                Occurrences; // occurrences of the symbol
spare::RealType                 CompCost;    // compactness cost
spare::RealType                 Cost;        // total cost
spare::RealType                 CardCost;    // cardinality/size cost
spare::RealType             Part;        // index of partition of the symbol
spare::RealType             Clust;       // index of the cluster of the symbol
spare::RealType                 CardClust;   // cardinality of the cluster of the symbol
std::vector<spare::RealType>    ThetaVec;    // vector for theta values

    /*
     * Default constructor
     */
    Granule() {}

    /*
     * Constructor prototype
     */
    Granule(typename spare::Sequence<AtomicElement>& rPrototype,
           spare::RealType  aRecogThreshold,
           spare::RealType  aClusterQuality) {

        if (rPrototype.size()==0) {
            throw std::logic_error("Empty prototype.");
        }

        if ((aRecogThreshold < 0) || (aRecogThreshold > 1)) {
            throw std::logic_error("Invalid DistThreshold.");
        }

        if ((aClusterQuality < 0) || (aClusterQuality > 1)) {
            throw std::logic_error("Invalid ClusterQuality.");
        }

        // Initialization
        mPrototype= rPrototype;
        mRecogThreshold= aRecogThreshold;
        mClusterQuality= aClusterQuality;
    }


    /*
     * Constructor with prototype defined through iterator
     */
    Granule(typename spare::Sequence<AtomicElement>::const_iterator iBegin,
            typename spare::Sequence<AtomicElement>::const_iterator iEnd,
            GdsReal aTheta,
            GdsReal aBeta) {

        if (iBegin >= iEnd) {
            throw std::logic_error("Invalid iterators.");
        }

        if ((aTheta < 0) || (aTheta > 1)) {
            throw std::logic_error("Invalid Theta.");
        }

        if ((aBeta < 0) || (aBeta > 1)) {
            throw std::logic_error("Invalid Beta.");
        }

        // Assign
        mPrototype.assign(iBegin, iEnd);
        mRecogThreshold= aTheta;
        mClusterQuality= aBeta;
    }


    // Prototype definition through iterators
    typename spare::Sequence<AtomicElement>::const_iterator begin() const { return mPrototype.begin(); }

    typename spare::Sequence<AtomicElement>::const_iterator end() const   { return mPrototype.end(); }

    // Prototype size
    typename spare::Sequence<AtomicElement>::size_type  size () const { return mPrototype.size(); }


    // Access to Prototype
    const typename spare::Sequence<AtomicElement>&  Prototype() const       { return mPrototype; }
    // Access to Prototype
    typename spare::Sequence<AtomicElement>&  Prototype()           { return mPrototype; }

    // Access to RecogThreshold
    const spare::RealType&   RecogThreshold() const { return mRecogThreshold; }

    // Access to ClusterQuality
    const spare::RealType&   ClusterQuality() const { return mClusterQuality; }
    // Access to RecogThreshold
    spare::RealType&         RecogThreshold()        { return mRecogThreshold; }

    // Access to ClusterQuality
    spare::RealType&         ClusterQuality()        { return mClusterQuality; }

private:


    // Structure that represents the symbol
    typename spare::Sequence<AtomicElement> mPrototype;

    // Recognition threshold
    spare::RealType                         mRecogThreshold;

    // Symbol quality
    spare::RealType                         mClusterQuality; };

   // Symbols ordering criteria
  template <class StructureType>
  inline bool operator<(const StructureType& S1, const StructureType& S2) {
            return (S1.ClusterQuality() > S2.ClusterQuality());
  }

当我编译错误是:
error C2039: 'ClusterQuality': is not a member of 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Granule<std::vector<spare::RealType,std::allocator<T>>>>>>'    

但是,如果我在Dev c++上编译相同的项目并使用c++11标志,则不会出现此错误。

我怎么解决这个问题?我必须使用Visual Studio。

最佳答案

您的问题是您的operator<是全局的并且太宽泛。我假设您在某处具有在 vector 上迭代并比较元素的代码。

template <class StructureType>
inline bool operator<(const Granule<StructureType>& S1, const Granule<StructureType>& S2) {
    return (S1.ClusterQuality() > S2.ClusterQuality());
}

或类似。

我可以在这里https://godbolt.org/g/3AR6kd复制它。我刚刚添加
int main()
{
    std::vector<int> t2;

    if (t2.end() < t2.begin())
    {
    }

    return 0;
}

关于c++ - Visual Studio 2015 C++项目错误C++ 11标准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47327736/

相关文章:

c++ - 默认的移动构造函数是否被认为是用户声明的?

visual-studio-2015 - 使用 xUnit 适配器在 TFS/VSO Build vNext 中运行单元测试

linux - 找不到 AX_CXX_COMPILE_STDCXX_11 宏

c# - .NET 4.6 RC x64 比 x86(发行版)慢两倍

visual-studio-2015 - 如何定义自定义键盘快捷键以在 VS 2015 中插入代码片段?

c++ - 使用std::find和std::string数组时出现的问题

c++ - 用于包括 guard 的名称?

c++ - 如何使用 xerces 向 xml 添加属性?

c++ - 使用具有相同名称的(无作用域的)基类枚举初始化派生类枚举

c++ - 我们可以对 std::array 使用传统的指针算法吗?