Object - std :: array - 构造函数初始化问题

时间:2014-02-04 04:44:01

标签: c++ arrays c++11

我正在编写一个周期表程序来帮助我理解课程。 我希望能够通过几个属性来显示/排序元素,例如它是金属,非金属还是非金属。我不确定要做什么,但我的第一个猜测是创建一个对象数组;但是,我在使用构造函数设置值时遇到了问题。

class Element{
    public:
       enum class groupNames {  HYDROGEN, ALKALI, ALKALINE, GROUP_THREE, GROUP_FOUR, GROUP_FIVE,
                                GROUP_SIX, GROUP_SEVEN, GROUP_EIGHT, GROUP_NINE, GROUP_TEN,
                                GROUP_ELEVEN,GROUP_TWELVE, GROUP_THIRTEEN, GROUP_FOURTEEN,
                                GROUP_FIFTEEN, CHALCOGEN, HALOGEN, NOBLE_GAS
                                };
        enum class orbitals {ORBITAL_NOTSET, S_BLOCK, P_BLOCK, D_BLOCK, F_BLOCK};
        enum class metal_status {METAL = 0, METALLOID, NONMETAL};
        Element();
        Element(int aNumber, int pNumber,groupNames groupnames, metal_status MetalStatus, orbitals Orbital,std::string eName, std::string eSybol);
        void displayProperties();

    private:
        groupNames groupNumber;
        orbitals orbital;
        metal_status metalStatus;
        std::string elementSymbol;
        std::string elementName;
        int atomicNumber;
        int periodNumber;
};

Element::Element()
{
    atomicNumber = 0;
    periodNumber = 0;
    groupNumber = groupNames::HYDROGEN;
    metalStatus = metal_status::METAL;
    orbital = orbitals::ORBITAL_NOTSET;
    elementName = "NULL";
    elementSymbol = "NULL";
}

Element::Element(int aNumber, int pNumber,groupNames groupnames, metal_status MetalStatus, orbitals Orbital,std::string eName, std::string eSymbol)
{
    groupNumber = groupnames;
    metalStatus = MetalStatus;
    orbital = Orbital;
    atomicNumber = aNumber;
    periodNumber = pNumber;
    elementName = eName;
    elementSymbol = eSymbol;
}

void Element::displayProperties()
{
    std::cout << elementName << ", " << elementSymbol << "\n"
              << "Group Number: " << as_integer(groupNumber) << "\n"
              << "Metal Status: " << as_integer(metalStatus) << "\n"
              << "Orbital: "      << as_integer(orbital) << "\n"
              << "Atomic Number: "<< atomicNumber << "\n"
              << "Period Number: "<< periodNumber;
}

以前的初始化方法 //工作正常,问题是我无法按属性排序

Element Hydrogen(1,1, Element::groupNames::HYDROGEN, Element::metal_status::NONMETAL, Element::orbitals::S_BLOCK, "Hydrogen", "H");
Element Helium(2, 1, Element::groupNames::NOBLE_GAS, Element::metal_status::NONMETAL, Element::orbitals::S_BLOCK, "Helium", "He");

std :: array方法 - 问题!

std::array<Element, 115> Elements =
{
    Elements[0],
    Elements[1](1,1, Element::groupNames::HYDROGEN, Element::metal_status::NONMETAL, Element::orbitals::S_BLOCK, "Hydrogen", "H")
};
  

错误:错误:无法调用'(std :: array :: value_type {aka Element})(int,int,Element :: groupNames,   Element :: metal_status,Element :: orbitals,const char [9],const char   [2])'

1 个答案:

答案 0 :(得分:2)

你可能需要两套大括号(我正在拉出我的头发,弄清楚clang在抱怨什么。)我建议统一初始化。另外,我为您的枚举添加Element作为资格,并将其更改为与您的班级定义中的名称相匹配。

std::array<Element, 115> Elements =
{{
    {},
    {1,1, Element::groupNames::HYDROGEN, Element::metal_status::NONMETAL, Element::orbitals::S_ORBITAL, "Hydrogen", "H"}
}};

或者,您可以尝试:

std::array<Element, 115> Elements
{  
  Element(), 
  Element(1,1, Element::groupNames::HYDROGEN, Element::metal_status::NONMETAL, Element::orbitals::S_ORBITAL, "Hydrogen", "H")  
};