【问题标题】:Enum Array as parameter c++枚举数组作为参数 c++
【发布时间】:2016-09-22 02:46:53
【问题描述】:

我是 C++ 新手,我想包含一个 Enum 值数组,但在 Microsoft Visual Studio 中出现语法错误。我不确定为什么会这样,任何帮助都非常感谢。错误号是 C2061,它指出“语法错误:标识符 'VerboseBinary'。这是代码:

头文件verbose_binary.h

    #pragma once
    #include <bitset>
    enum class VerboseBinary : int {
        One,
        Two,
        Four,
        Eight,
        Sixteen,
        Null,
    };

    void convert(std::bitset<5> bs, VerboseBinary aVB[6]);

verbose_binary.cpp

        #include "verbose_binary.h"
        #include "stdafx.h"
        #include <bitset>
        #include <string>
        #include <iostream>


        void convert(std::bitset<5> bs, VerboseBinary aVB[6]) {
            VerboseBinary VBArray[6] = {
                VerboseBinary:: One,
                VerboseBinary:: Two,
                VerboseBinary:: Four,
                VerboseBinary:: Eight,
                VerboseBinary:: Sixteen,
                VerboseBinary:: Null
            };

            for (int i = 0; i < 5; i++) {
                if (bs.test(i)) {
                    aVB[i] = VBArray[i];
                }
                else {
                    aVB[i] = VerboseBinary::Null;
                }

            }
            aVB[5] = VerboseBinary::Null;

        }

主要

#include "stdafx.h"
#include <iostream>
#include <iostream>
#include <bitset>

#include "verbose_binary.h"

int main() {
    int a, b;
    std::bitset<5> aBs, bBs;
    std::cout << "Enter two numbers between 0-31:" << std::endl;
    std::cin >> a >> b;
    if (a<0 || a>31) return -1;
    if (b<0 || b>31) return -2;
    aBs = static_cast<std::bitset<5>>(a);
    bBs = static_cast<std::bitset<5>>(b);
    // std::cout << aBs << " " << bBs << std::endl;
    VerboseBinary aVB[6];
    VerboseBinary bVB[6];
    convert(aBs, aVB);
    convert(bBs, bVB);
    return 0;
}

【问题讨论】:

  • 当我还是初学者时,毫无理由地投反对票并不是很令人鼓舞。
  • 请仔细阅读stackoverflow.com/help/mcve你的代码不是最小的。您没有编写错误代码的行号。 (我没有对你投反对票)
  • 您使用的是什么版本的 Visual Studio? enumclass 在 VS 2012 中可用。此外,枚举定义的末尾有一个杂散的逗号。此外,stdafx.h 应该出现在 verbose_binary.cpp 中的任何其他包含之前。 Main 对 &lt;iostream&gt; 具有良性双重包含。
  • @ChristopherOicles 非常感谢!如果您希望我接受,请将其发布为答案。
  • 在删除所有 Microsoft ese gobbledygook(即 stdafx.h 愚蠢)后,显示的代码使用 gcc 6.1.1 编译没有错误。错误出现在未显示的代码中,或者这是编译器错误。

标签: c++ c++11 visual-c++ enums


【解决方案1】:

大声笑,所以看起来这些问题之一是导致错误的原因:

  • 您使用的是什么版本的 Visual Studio?带类的枚举随 VS 2012 提供。

  • 此外,在枚举定义的末尾有一个杂散的逗号。

  • 此外,stdafx.h 应出现在 verbose_binary.cpp 中的任何其他包含之前。

  • Main 对 &lt;iostream&gt; 具有良性双重包含

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 2012-03-27
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多