Enum作为c++参数的数组

Enum Array as parameter c++

本文关键字:数组 参数 c++ 作为 Enum      更新时间:2023-10-16

我是c++的新手,我想包含枚举值的数组,我在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;
}

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

  • 您使用的是哪个版本的Visual Studio ?带类的enum在VS 2012中可用。

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

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

  • Main对<iostream>具有良性双包合