枚举数组作为参数c ++

时间:2016-09-22 02:46:53

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

我是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;
}

1 个答案:

答案 0 :(得分:1)

大声笑,所以看起来其中一个问题是错误的:

  • 您使用的是哪个版本的Visual Studio?随着VS 2012的上课,enum with class。

  • 此外,在枚举结尾处有一个逗号逗号。

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

  • Main为<iostream>

  • 提供良性双重包含