2014-06-18 2 views
-2

이 프로그램은 공백 문자 앞에 ': cin >> buffer'을 사용하여 문자를 읽고 싶을 때 작동했습니다. 그러나 공백을 포함하여 사용자가 입력 한 모든 것을 읽고 싶었습니다. 그래서, ">>"를 사용하여 getline 호출로 변경했습니다. 나는 올바른 매개 변수를 전달하는 것으로 보입니다. #include와 #include를 수행했습니다. 내가 컴파일 오류 메시지는 다음과 같습니다 나사 경우getline에 맞는 함수가 없습니다.

#include <iostream> 
#include <cstdio> 
#include <string> 
#include <getopt.h> 
#include "Driver.hpp" 
#include "SymTab.hpp" 

using namespace std; 

#ifdef NULL 
#undef NULL 
#define NULL 0 
#endif 

ostream & operator << (ostream & stream, const Student & stu) { 
     return stream << "name: " << stu.name 
       << " with studentnum: " << stu.studentnum; 
} 

int main (int argc, char * const * argv) { 
     char buffer[BUFSIZ]; 
     char command; 
     long number; 
     char option; 

     while ((option = getopt (argc, argv, "x")) != EOF) { 

     switch (option) { 
       case 'x': SymTab<Student>::Set_Debug_On(); 
         break; 
       }  
     } 

     SymTab<Student> ST; 
     ST.Write (cout << "Initial Symbol Table:\n"); 

     while (cin) { 
       command = NULL;   // reset command each time in loop 
       cout << "Please enter a command ((i)nsert, " 
         << "(l)ookup, (r)emove, (w)rite): "; 
       cin >> command; 

       switch (command) { 

       case 'i': { 
         cout << "Please enter student name to insert: "; 
         getline(cin, buffer); 

         cout << "Please enter student number: "; 
         cin >> number; 

         Student stu (buffer, number); 

         // create student and place in symbol table 
         ST.Insert (stu); 
         break; 
       } 

     } 

     ST.Write (cout << "\nFinal Symbol Table:\n"); 
} 

이 내 첫 번째 게시물, 그래서 알려되는 : 여기

Driver.cpp: In function ‘int main(int, char* const*)’: Driver.cpp:49:44: error: no matching function for call to ‘getline(std::istream&, char [1024])’ getline(cin, buffer); ^Driver.cpp:49:44: note: candidates are: In file included from /usr/include/wchar.h:4:0, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/cwchar:44, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/postypes.h:40, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/iosfwd:40, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/ios:38, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/ostream:38, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/iostream:39, from Driver.cpp:1: /usr/include/sys/stdio.h:37:9: note: ssize_t getline(char**, size_t*, FILE*) ssize_t _EXFUN(getline, (char **, size_t *, FILE *)); ^/usr/include/sys/stdio.h:37:9: note: candidate expects 3 arguments, 2 provided In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:52:0, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/locale_classes.h:40, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/ios_base.h:41, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/ios:42, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/ostream:38, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/iostream:39, from Driver.cpp:1: /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/basic_string.h:2793:5: note: template std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&) getline(basic_istream<_CharT, _Traits>& __is, ^

^/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/basic_string.tcc:1068:5: 

note: template argument deduction/substitution failed: Driver.cpp:49:44: note: mismatched types ‘std::basic_string<_CharT, _Traits, _Alloc>’ and ‘char [1024]’ getline(cin, buffer); ^Makefile:24: recipe for target 'Driver.o' failed make: * [Driver.o] Error 1

이 케이스의 일부가 아닌 제거 물건 코드입니다 내 서식을 정리하고 나를 돕기 위해 더 많은 정보가 필요한지 확인하십시오. 감사!

+2

'std :: getline'은 자동으로 사용해야하는'std :: string'을위한 것이지만 더 걱정스러운 것은 전체'NULL' 스피어입니다. 그 부분은 매우 나쁘다. 'cstddef' 또는 다른 특정 헤더 ('cstdio'와 같은)를 포함하고'NULL'은 0으로 평가되는 것으로 올 것입니다. – ghostofstandardspast

+1

@ghost C++에서'NULL'을 0으로 평가하는 것이 당신이 소리를내는 것만큼 나쁘지는 않습니다. 예, 표준 라이브러리 헤더가 처리합니다. 그러나 바람직하지 않은 방식으로 'NULL'을 정의하는 (잘못 작성된) C 헤더를 포함하는 것이 일반적입니다. 물론'nullptr'을 사용하여 모든 것을 해결할 수 있습니다. –

+0

좋아요, 그래서 cstdlib를 포함 시켰고 이제는 NULL을 정의 할 필요가 없습니다. getline의 주제에 대해 getline을 자동으로 stdd ::로 시작해야하는 이유는 무엇입니까? – user3750325

답변

6

std::getline()의 모든 과부하는 (basic) istreamstring입니다. 문제를 해결하려면 char buffer []std::string buffer으로 변경하고 #include <string>을 기억하십시오.

+0

솔루션은 괜찮지 만 멤버 함수 ['getline'] (http://en.cppreference.com/w/cpp/io/basic_istream/getline)가 있습니다. –

+0

그 문제가 해결되었습니다! 그러나 이제는 "Student"를 다루는 모든 행에서 오류가 발생합니다. (네임 스페이스를 다뤄야했기 때문에 몇 분기가 걸렸습니다.) – user3750325

관련 문제