如何摆脱C ++中的枚举不匹配错误?

时间:2018-08-09 20:46:43

标签: c++ templates enums

在我发现的书中,有一个代码可以计算给定数字的平方根,但是代码无法编译。实例化模板时有很多错误。这是代码:

<div id="div2">
  <section>
    <div class="cover-photo">
      <h1 id="about" class="lawyer-header">Lawyer Turned Developer</h1>
    </div>

    <div class="profile-pic-class">
      <img class="ryan-profile" src="images/ryan-profile.jpg" alt "Photo of Ryan McAvoy, Developer">
    </div>
    <div class="scroll-box1">
      <figure class="p-one">
        <p>After successfully completing a Degree and Masters Degree in Law, I began searching for a job within the legal industry. Unfortunately (later found to be fortunate), this proved to be very difficult. Having some previous sales experience, I decided
          to accept recruitment job offer with Mortimer Spinks. At the time, I did not know what recruitment entailed nor did I know anything about the industry I was soon to be recruiting in, technology.</p>
        <p>Working as a recruiter was an eye-opening experience into the world of technology. It was only through working at Mortimer Spinks I began to realise how much technology is integrated into our daily lives. Moreover, I discovered the depth of work
          required to develop such technologies. </p>
        <p>The part I most enjoyed about recruitment was meeting with developers and talking about the latest technologies and their personal projects. I’ve always been quite entrepreneurial and once I discovered the capabilities of technology, I began coming
          up with solutions to everyday problems. Naturally, I did not have the skills or finance to build these ideas; having worked in recruitment I know how expensive building websites and apps can be. I also thought given my age (26 at the time),
          it would be too late to learn how to code. I decided to put these ideas to the back of my brain and focus on my career instead. </p>
        <p>Eventually, I managed secure a paralegal position within a highly respectable law firm which has been involved in some of England’s most notable cases in the last 30 years. I enjoy my current job and still like law however, these ideas are still
          festering at the back of my brain. I have considered the possibility of becoming a solicitor and then using the money from that to fund the various ideas. It was at this point I thought, why not become a developer yourself. I’m extremely passionate
          about technology and really believe, if not already, the tech industry is the future. Why not work as a developer, learn the required skills and network with the people within the industry I wish to one day establish my business. It does not
          make sense to have a career in law with the knowledge that in five years time I plan to establish a business within a completely different industry. </p>
      </figure>

许多警告是

#include <iostream>

template
<int N, int LO=1, int HI=N> class Sqrt {
public:
    enum { mid = (LO+HI+1)/2 };

    enum { result = (N<mid*mid) ? Sqrt<N,LO,mid-1>::result : Sqrt<N,mid,HI>::result };
};

template<int N, int M> class Sqrt<N,M,M> {
public:
    enum { result=M };
};

int main()
{
    std::cout << Sqrt<5>::result << std::endl;
}

稍后我测试该警告并不严重,例如在代码中

enumeral mismatch in conditional expression 'Sqrt<5,1,1>::< anonymous enum> vs 'Sqrt<5,2,2>::< anonymous enum>' [-Wenum-compare]    

我得到同样的警告,但是代码可以编译。造成混乱的原因是,如果将“ -Wenum-compare”放入编译器选项中,则会编译第一个代码。真正的问题是什么?

2 个答案:

答案 0 :(得分:1)

您可以使用enum代替static constexpr int

template <int N, int LO=1, int HI=N> struct Sqrt {
    static constexpr int mid = (LO+HI+1)/2;
    static constexpr int result = (N<mid*mid) ? Sqrt<N,LO,mid-1>::result : Sqrt<N,mid,HI>::result;
};

template<int N, int M> struct Sqrt<N,M,M> {
    static constexpr int result=M;
};

答案 1 :(得分:1)

要摆脱警告,可以将枚举转换为它们的整数值。这意味着您有两个整数,而不是两个不同的枚举。

template
<int N, int LO=1, int HI=N> class Sqrt {
public:
    enum { mid = (LO+HI+1)/2 };

    enum { result = (N<mid*mid) ? static_cast<int>(Sqrt<N,LO,mid-1>::result) : static_cast<int>(Sqrt<N,mid,HI>::result) };
};

template<int N, int M> class Sqrt<N,M,M> {
public:
    enum { result=M };
};

int main()
{
    std::cout << Sqrt<5>::result << std::endl;
}

将在没有警告的情况下进行编译。至于即使编译后仍收到这些警告的原因,请参见:Why do I get an error when directly comparing two enums?