2017-11-04 1 views
0

이 코드는 내가 작업하고있는 작업의 일부이지만 getline이 "오버로드 된 함수의 인스턴스가 없습니다"오류를 반환하는 이유를 알 수 없습니다. 나는 여기에 관련 코드를 포함 시켰고 라인은 바닥을 향하고있다. 동물에 대한 "이름"을 얻는 데 도움이된다면 나는 그것이 단순한 것이라고 확신하면서 높이 평가할 것입니다. 고맙습니다.배열에 getline (cin)과 관련된 문제가 있습니다.

#include <iostream> // provides access to cin and cout 
#include <array>// provides access to std:array 
#include <string> // required for getline 

//--end of #include files----------- 
//---------------------------------- 

using namespace std; 
//---------------------------------- 

//**begin global constants********** 
const int arraySize = 4; // **there is a subtle bug here (needs "const") 
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; 
}; 

//--end of global constants--------- 
//---------------------------------- 


//**begin main program************** 
int main() 
{ 
    // Initialization 
    char myCString[arraySize] = { 0 }; 
    char myOtherCString[] = { "Yet another string" }; 
    int myInt[4] = { 27, 39, 0, 42 }; 
    string myString; 
    MyStruct aStruct = { 4,3.5,"Dog", Dog}; 
    int x; 
    int * pX; 
    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); 
    } 
+0

여기 실수로 잘못 생각합니다 .Animals [i] .d = MyEnum (rand() % 4); cout << "이름 입력 :"; getline (cin, Animals [i] .c); – user2181624

답변

2

std::string::getline()의 두 번째 인수는 std::string 할 필요가 있지만, 대신 그것을 MyEnum을 제공하고 있습니다.

std::string을 읽고 MyEnum을 저장하려면 하나에서 다른 것으로 변환 할 찾아보기 테이블이 있어야합니다.

EDIT : 실제로 방금 cd이 잘못되었습니다. d에 임의의 숫자를 지정하고 getline()에서 c까지 읽은 문자열을 지정하려고합니다.

1

잘못된 매개 변수 인 getline을 사용하고 있습니다. MyEnumstring이 아닙니다. 여기에 사용하십시오 :

istream& getline (istream& is, string& str, char delim); 
istream& getline (istream&& is, string& str, char delim); 
istream& getline (istream& is, string& str); 
istream& getline (istream&& is, string& str); 
관련 문제