C ++ getline错误

时间:2016-09-11 18:21:37

标签: c++

我收到此错误,我无法弄清楚:

  

没有重载功能的实例" getline"匹配参数列表       参数类型是:(std :: istream,MyEnum)

代码

enum MyEnum // Needs to be before the struct that uses it
{
    Dog, Cat, Fish, Squirrel
};
struct MyStruct
{
    int a;
    float b;
    string c;
    MyEnum d;
};
int main()
{
    char myCString[arraySize] = { 0 };
    char myOtherCString[] = { "Yet another string" };
    int myInt[4] = { 27, 39, 0, 42 };
    string myString;
    MyStruct aStruct = { 4, 3.5, "Dogs", Dog };
    int x;
    int * pX = &x;
    array <MyStruct, arraySize> Animals;
    // Storing values in uninitialized variables
    myCString[0] = 'A';
    myString = "A third string";
    x = 4;
    for (int i = 0; i < arraySize; i++)
    {
        Animals[i].a = rand() % 10;
        Animals[i].b = rand() % 100 / 100.0;
        Animals[i].c = MyEnum(rand() % 4);
        cout << "Enter a name: ";
        getline(cin, Animals[i].d);
    }

1 个答案:

答案 0 :(得分:1)

您可能希望在Animals[i].c中获得输入,因为cstringd不是。{/ p>

此外,您可能希望将MyEnum(rand() % 4);分配给Animals[i].d

for (int i = 0; i < arraySize; i++)
{
    Animals[i].a = rand() % 10;
    Animals[i].b = rand() % 100 / 100.0;
    Animals[i].c = MyEnum(rand() % 4);
    //        ^^^
    // Change it to Animals[i].d
    cout << "Enter a name: ";
    getline(cin, Animals[i].d);
    //                     ^^^
    // Change it to Animals[i].c
}

这只是一个糟糕的命名风格,你应该有一些有意义的变量名称,有一些有意义的名字,你可能永远不会有这种错误。